I am looping over many data files and reading in the data with F <- read.table(filename) to read in a 22000 by 15 matrix. Works fine on the first matrix F, but I get the following error when the second file is read into F: Error in "row.names<-.data.frame"(*tmp*, value = row.names) : duplicate row.names are not allowed I have tried picking a column of the matrix and making that my rownames by doing rownames <- as.vector(F[,4]) -but that does not work. Each row in my matrix is not unique. Any suggestions greatly appreciated. John
I think you need to be a bit more specific for us to be able to help you. At least provide a toy problem which replicates the error. Does this do what you want? dat1 <- data.frame (x=rnorm(100), y=rnorm(100)) #make up some data dat2 <- data.frame (x=rnorm(100), y=rnorm(100)) write.table(dat1, file="test1.txt") #write data to files write.table(dat2, file="test2.txt") F<- NULL # Initialise for (i in 1:2) F <- rbind(F, read.table(paste("test", i, ".txt", sep=""))) F <- data.frame(F, row.names=NULL) # clean up row names. Hope this helps, Simon. Simon Blomberg, PhD Depression & Anxiety Consumer Research Unit Centre for Mental Health Research Australian National University http://www.anu.edu.au/cmhr/ Simon.Blomberg at anu.edu.au +61 (2) 6125 3379> -----Original Message----- > From: John Smith [mailto:JSmith at telicmanagement.com] > Sent: Thursday, 17 July 2003 10:06 AM > To: 'r-help at stat.math.ethz.ch' > Subject: [R] duplicate row.names > > > I am looping over many data files and reading in the data > with F <- > read.table(filename) to read in a 22000 by 15 matrix. Works > fine on the > first matrix F, but I get the following error when the second > file is read > into F: > > Error in "row.names<-.data.frame"(*tmp*, value = row.names) : > duplicate row.names are not allowed > > I have tried picking a column of the matrix and making that > my rownames by > doing rownames <- as.vector(F[,4]) > -but that does not work. > > Each row in my matrix is not unique. > > Any suggestions greatly appreciated. > > John > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
The row.names argument (if present) defines which column is to be used as the row names. The default in read.table() is missing row.names and hence row.names is not detected. The row names (if present) need to be unique. Giving us your code would have been helpful, but I am guessing you have set the row.names to some number and hence it would be complain if it not unique. Try : 1. Setting row.names=FALSE 2. Assigning a unique identifier to each row 2. Manually rename the redundant rows Your problem is with reading the data itself, and ` rownames <- as.vector(F[,4]) ` just creates another unrelated variable. Regards, Adai. -----Original Message----- From: John Smith [mailto:JSmith at telicmanagement.com] Sent: Thursday, July 17, 2003 8:06 AM To: 'r-help at stat.math.ethz.ch' Subject: [R] duplicate row.names I am looping over many data files and reading in the data with F <- read.table(filename) to read in a 22000 by 15 matrix. Works fine on the first matrix F, but I get the following error when the second file is read into F: Error in "row.names<-.data.frame"(*tmp*, value = row.names) : duplicate row.names are not allowed I have tried picking a column of the matrix and making that my rownames by doing rownames <- as.vector(F[,4]) -but that does not work. Each row in my matrix is not unique. Any suggestions greatly appreciated. John ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
row.names=NULL seems to have fixed my problem. strange that NULL uses row numbers and not specifying uses row numbers, but NULL poses no problems and the latter does. Thanks to you and everyone else who responded. John -----Original Message----- From: Simon Blomberg [mailto:Simon.Blomberg at anu.edu.au] Sent: Wednesday, July 16, 2003 9:50 PM To: John Smith; r-help at stat.math.ethz.ch Subject: RE: [R] duplicate row.names I think you need to be a bit more specific for us to be able to help you. At least provide a toy problem which replicates the error. Does this do what you want? dat1 <- data.frame (x=rnorm(100), y=rnorm(100)) #make up some data dat2 <- data.frame (x=rnorm(100), y=rnorm(100)) write.table(dat1, file="test1.txt") #write data to files write.table(dat2, file="test2.txt") F<- NULL # Initialise for (i in 1:2) F <- rbind(F, read.table(paste("test", i, ".txt", sep=""))) F <- data.frame(F, row.names=NULL) # clean up row names. Hope this helps, Simon. Simon Blomberg, PhD Depression & Anxiety Consumer Research Unit Centre for Mental Health Research Australian National University http://www.anu.edu.au/cmhr/ Simon.Blomberg at anu.edu.au +61 (2) 6125 3379> -----Original Message----- > From: John Smith [mailto:JSmith at telicmanagement.com] > Sent: Thursday, 17 July 2003 10:06 AM > To: 'r-help at stat.math.ethz.ch' > Subject: [R] duplicate row.names > > > I am looping over many data files and reading in the data > with F <- > read.table(filename) to read in a 22000 by 15 matrix. Works > fine on the > first matrix F, but I get the following error when the second > file is read > into F: > > Error in "row.names<-.data.frame"(*tmp*, value = row.names) : > duplicate row.names are not allowed > > I have tried picking a column of the matrix and making that > my rownames by > doing rownames <- as.vector(F[,4]) > -but that does not work. > > Each row in my matrix is not unique. > > Any suggestions greatly appreciated. > > John > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >