从R中读取CSV文件

3

我刚开始接触R语言,并且想要读取一个csv文件。但是当我尝试读取它的时候却遇到了错误。

我的csv文件如下所示:

,Zbot,Sirefef,Fareit,Winwebsec,FakeSysdef,Winwebsec,Winwebsec,Winwebsec,Fareit,Fareit,Sirefef,Winwebsec,Winwebsec,Winwebsec,Winwebsec
Zbot,0,134,45,651,182,245,986,64,63,34,134,166,52,337,225
Sirefef,142,0,124,679,200,273,1018,156,125,122,164,198,120,371,257
Fareit,48,124,0,644,166,234,978,82,64,51,135,167,49,338,224
Winwebsec,651,499,470,0,575,556,1087,525,490,485,501,511,483,600,582
FakeSysdef,178,172,143,535,0,311,1052,196,163,152,204,234,154,405,285
Winwebsec,245,199,168,478,229,0,997,217,186,183,199,209,183,348,272
Winwebsec,986,752,719,821,784,727,0,774,739,734,750,760,734,851,829
Winwebsec,80,160,85,506,179,204,757,0,100,85,173,205,89,376,264
Fareit,65,95,32,468,141,164,715,78,0,57,135,165,59,336,226
Fareit,52,122,51,468,143,166,717,68,40,0,135,163,49,336,224
Sirefef,136,118,85,449,150,147,696,123,83,83,0,146,100,317,207
Winwebsec,164,138,105,449,170,145,696,143,103,103,80,0,118,315,215
Winwebsec,52,116,51,466,143,166,717,66,42,32,83,103,0,336,226
Winwebsec,335,267,234,496,301,246,745,272,232,234,213,213,234,0,346
Winwebsec,225,204,171,519,228,217,774,207,169,169,150,160,169,291,0

当我在RStudio中使用这个命令时,我收到了一个错误提示:

> tb = read.csv("/home/hossein/Documents/LiClipse Workspace/test.csv", row.names = 1);

错误:

读取数据表时出错,文件名为file,表头为header,分隔符为sep,引号为quote。错误信息为:不允许重复的行名(row.names)。

我尝试修复这个错误并使用了以下命令:

 > tb = read.csv("/home/hossein/Documents/LiClipse Workspace/test.csv", row.names = NULL);

但是当我查看输出时,发现它不能保持正方形矩阵的结构。请问应该怎么做呢?


1
首先,数据集的第一行以逗号开头。其次,为什么不尝试使用RStudio加载数据集的GUI功能呢?如果这样做,您可能会更有机会检测到为什么您的数据无法正确加载。 - Abdou
3个回答

2

R不支持重复的行名称,参见?row.names

所有数据框都有一个行名称属性,它是一个长度等于行数且没有重复或缺失值的字符向量。

基于此,您可以导入数据,然后使用函数make.names使行名称唯一。这有点丑陋,但我认为它可以解决您的问题。请看下面的示例,其中包含您提供的数据。

table <- read.csv("data.csv", row.names = NULL)

### Save row names in a separte object:
rows <- table$X

### Remove the colunm with row.names:
table <- table[,-1]

### Create unique Row.names with the function `make.names`
rownames(table) <- make.names(rows, unique=TRUE)

### Check results:
dim(table)
#> [1] 15 15
head(table)
#>             Zbot Sirefef Fareit Winwebsec FakeSysdef Winwebsec.1
#> Zbot           0     134     45       651        182         245
#> Sirefef      142       0    124       679        200         273
#> Fareit        48     124      0       644        166         234
#> Winwebsec    651     499    470         0        575         556
#> FakeSysdef   178     172    143       535          0         311
#> Winwebsec.1  245     199    168       478        229           0
#>             Winwebsec.2 Winwebsec.3 Fareit.1 Fareit.2 Sirefef.1
#> Zbot                986          64       63       34       134
#> Sirefef            1018         156      125      122       164
#> Fareit              978          82       64       51       135
#> Winwebsec          1087         525      490      485       501
#> FakeSysdef         1052         196      163      152       204
#> Winwebsec.1         997         217      186      183       199
#>             Winwebsec.4 Winwebsec.5 Winwebsec.6 Winwebsec.7
#> Zbot                166          52         337         225
#> Sirefef             198         120         371         257
#> Fareit              167          49         338         224
#> Winwebsec           511         483         600         582
#> FakeSysdef          234         154         405         285
#> Winwebsec.1         209         183         348         272
as.dist(table)
#>             Zbot Sirefef Fareit Winwebsec FakeSysdef Winwebsec.1
#> Sirefef      142                                                
#> Fareit        48     124                                        
#> Winwebsec    651     499    470                                 
#> FakeSysdef   178     172    143       535                       
#> Winwebsec.1  245     199    168       478        229            
#> Winwebsec.2  986     752    719       821        784         727
#> Winwebsec.3   80     160     85       506        179         204
#> Fareit.1      65      95     32       468        141         164
#> Fareit.2      52     122     51       468        143         166
#> Sirefef.1    136     118     85       449        150         147
#> Winwebsec.4  164     138    105       449        170         145
#> Winwebsec.5   52     116     51       466        143         166
#> Winwebsec.6  335     267    234       496        301         246
#> Winwebsec.7  225     204    171       519        228         217
#>             Winwebsec.2 Winwebsec.3 Fareit.1 Fareit.2 Sirefef.1
#> Sirefef                                                        
#> Fareit                                                         
#> Winwebsec                                                      
#> FakeSysdef                                                     
#> Winwebsec.1                                                    
#> Winwebsec.2                                                    
#> Winwebsec.3         757                                        
#> Fareit.1            715          78                            
#> Fareit.2            717          68       40                   
#> Sirefef.1           696         123       83       83          
#> Winwebsec.4         696         143      103      103        80
#> Winwebsec.5         717          66       42       32        83
#> Winwebsec.6         745         272      232      234       213
#> Winwebsec.7         774         207      169      169       150
#>             Winwebsec.4 Winwebsec.5 Winwebsec.6
#> Sirefef                                        
#> Fareit                                         
#> Winwebsec                                      
#> FakeSysdef                                     
#> Winwebsec.1                                    
#> Winwebsec.2                                    
#> Winwebsec.3                                    
#> Fareit.1                                       
#> Fareit.2                                       
#> Sirefef.1                                      
#> Winwebsec.4                                    
#> Winwebsec.5         103                        
#> Winwebsec.6         213         234            
#> Winwebsec.7         160         169         291

谢谢您的回答,但问题在于当使用Null时,通过使用此命令 d = as.dist(tb),我得到了以下错误提示:警告信息:1:在as.dist.default(tb)中:强制引入NAs 2:在as.dist.default(tb)中:非方阵矩阵。 - Alex
问题在于不允许重复的行名称。请参阅?read.csv。但是您可以使用make.names来“解决”这个问题。我会更新答案,然后您告诉我它是否适用于您。干杯 - Gustavo B Paterno
已更新答案...请看是否有帮助。 - Gustavo B Paterno

1
您可以按照以下几个简单步骤完成此操作:

d = read.csv('path/to/test.csv') # import the data
row.names(d) = make.unique(as.character(d[, 1])) # create the row names from the first column
d = d[, -1] # remove the first column now that you don't need it anymore

这将保持您的方阵不变:

dim(d) # still a 15x15 matrix

我按照你说的做了,但是我得到了不同的尺寸:[1] 15 16。 - Alex
1
我刚刚仔细检查了一下,使用你复制的CSV文件仍然得到了15x15的结果... - CephBirk

0

第一行以空字段开头,因此使用skip = 1参数可能很方便,也许是因为read.csv不将输入函数理解为矩形数组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接