Why read.csv in r shift all my data by a column?

跟風遠走 提交于 2019-12-12 01:19:08

问题


I'm trying to import a csv file in R. I have done it several times but with this specific file it returns me a error.

On the first row of the csv I have the names of the colums that are in part text and in part numbers, as they represent month, year, and the numbers of some observational points. The .csv file, even if larger, looks as follows:

Mo,Yr2,4,10,32,38,41,60,63,82

9,1980,    6.0,    0.2,    0.7,    1.0,    0.4,    0.7,    0.4,    1.5

10,1980,   25.1,   39.7,   41.4,   15.5,   20.8,   43.6,   37.1,   17.8

11,1980,   11.5,    8.6,   23.6,    7.5,   15.6,   12.2,   13.4,    7.6

12,1980,   59.6,   90.0,  103.9,   50.0,   67.1,  109.2,   81.6,   48.4  

I have tried the following getting an error:

> m <- read.csv(file="my_file.csv", sep=",",head=TRUE)

  Error in read.table(file = "my_file.csv", sep = ",", head = TRUE) : 
  duplicate 'row.names' are not allowed

so I have tried:

> m <- read.csv(file="my_file.csv", sep=",",head=TRUE,row.names=NULL)

> m

    row.names   Mo   Yr2    X4   X10   X32   X38   X41   X60   X63   X82

1           9 1980   6.0   0.2   0.7   1.0   0.4   0.7   0.4   1.5   NA

2          10 1980  25.1  39.7  41.4  15.5  20.8  43.6  37.1  17.8   NA

3          11 1980  11.5   8.6  23.6   7.5  15.6  12.2  13.4   7.6   NA

4          12 1980  59.6  90.0 103.9  50.0  67.1 109.2  81.6  48.4   NA

Can someone tell me what is the problem? Thanks in advance


回答1:


Have you used count.fields to see if all lines have the same number of delimiters? table(count.fields( ..)) is a useful check.

I've seen the problem you describe when there are a different number of delimiters on the header row than in the rest of the file.



来源:https://stackoverflow.com/questions/12585042/why-read-csv-in-r-shift-all-my-data-by-a-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!