Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate 'row.names' error reading table. row.names=NULL shifts columns

Tags:

r

This is similar to read.csv row.names and to https://stackoverflow.com/questions/12425599/duplicated-row-names , but I do not see answers that help.

Problem: Trying to read in a file which contains duplicate numbers in first column, but shifts the column headers when row.names=NULL.

I'm trying to read the following file into R

TripId  VID TspVID  VWT VCLS    Week

201110041426    2226    33889   1   0   41

201110041501    2226    33889   1   0   41

201110041510    2226    33889   1   0   41

201110041557    2226    33889   1   0   41

(this is a small excerpt from Excel of a CSV file with many thousands of rows and ~200 columns. There are the same number of entries in the first row as in all others. There are duplicates in the first row. The columns don't line up with the labels in this view, but they do in CSV space.)

The command

> lm.table  <- read.table(file= file.in, sep=",", header=TRUE)
Error in read.table(file = file.in, sep = ",", header = TRUE) : 
  duplicate 'row.names' are not allowed

doesn't work. Using the first column for row.names implies that the first row has less values than the others, which is not the case. I certainly don't want the first column as row.names.

I try to set row.names=NULL

> lm.table  <- read.table(file= file.in, sep=",", header=TRUE, row.names=NULL)

which runs, but the columns have been shifted

> head(lm.table)

     row.names TripId   VID TspVID VWT VCLS       Week     Date TimeStart TimeEnd     Lat1

1 201110010006   2226 33889      1   0   40 2011/09/30 17:06:37  17:25:16 47.5168 -122.209

2 201110010028   2226 33889      1   0   40 2011/09/30 17:28:45  17:43:14 47.5517 -122.058

3 201110010000   2231 45781      1   0   40 2011/09/30 17:00:00  18:02:30 32.9010 -117.193

4 201110011407   2231 45781      1   0   40 2011/10/01 07:07:57  07:48:17 32.7044 -117.004

Note that the new column name "row.names has been introduced and the entire row shifted right.

Here is the tail end of the > head(lm.table) result. It's shifted the column labels onto an undefined column (I think this also shows the number of column labels = number of columns, which is also true from inspection.)

      FVavR FVstdR FIdlR

1  3.959140      2    NA

2  5.285770     20    NA

3  4.274140     26    NA

Any idea why I get the shifting in the columns and how to not shift and have the row.names simply be ascending numbers?

like image 780
Chris Wilson Avatar asked Nov 05 '12 20:11

Chris Wilson


1 Answers

had the same problem. just added this line:

colnames(rec) <- c(colnames(rec)[-1],"x")
rec$x <- NULL
like image 140
adrianoesch Avatar answered Sep 21 '22 20:09

adrianoesch