问题
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