I am using rbind to concatenate two dataframes, but after the rbind I get a strange "double" index. I am using rbind a number of times to make one large dataset. Is there a way to prevent the "double" index? Or is there some other way to concatenate the data (it looks like merge only works by columns)? Here is an example of the "double" index.>numbers<-(1:5) >letters<-matrix(c("a","b","c","d","e"),5,1) >data<-data.frame(letters,numbers) >doubledata<-rbind(data,data) > doubledataletters numbers 1 a 1 2 b 2 3 c 3 4 d 4 5 e 5 11 a 1 22 b 2 33 c 3 44 d 4 55 e 5
> df1 <- data.frame(a=1:2)> df2 <- rbind(df1, df1) > df2 a 1 1 2 2 11 1 22 2 > rownames(df2) <- 1:4 hope this helps. spencer graves Ann Hess wrote:> I am using rbind to concatenate two dataframes, but after the rbind I get > a strange "double" index. > > I am using rbind a number of times to make one large dataset. Is there a > way to prevent the "double" index? Or is there some other way to > concatenate the data (it looks like merge only works by columns)? > > Here is an example of the "double" index. > > >>numbers<-(1:5) >>letters<-matrix(c("a","b","c","d","e"),5,1) >>data<-data.frame(letters,numbers) >>doubledata<-rbind(data,data) >>doubledata > > letters numbers > 1 a 1 > 2 b 2 > 3 c 3 > 4 d 4 > 5 e 5 > 11 a 1 > 22 b 2 > 33 c 3 > 44 d 4 > 55 e 5 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
When you rbind() `data' and `data', the two dataframes have the exact same rownames so rbind() tries to modify them to make the combined dataframe have unique rownames. If you don't want this, just use rownames()<- to modify the row names. Such as, rownames(doubledata) <- 1:nrow(doubledata) -roger _______________________________ UCLA Department of Statistics http://www.stat.ucla.edu/~rpeng On Mon, 21 Apr 2003, Ann Hess wrote:> I am using rbind to concatenate two dataframes, but after the rbind I get > a strange "double" index. > > I am using rbind a number of times to make one large dataset. Is there a > way to prevent the "double" index? Or is there some other way to > concatenate the data (it looks like merge only works by columns)? > > Here is an example of the "double" index. > > >numbers<-(1:5) > >letters<-matrix(c("a","b","c","d","e"),5,1) > >data<-data.frame(letters,numbers) > >doubledata<-rbind(data,data) > > doubledata > letters numbers > 1 a 1 > 2 b 2 > 3 c 3 > 4 d 4 > 5 e 5 > 11 a 1 > 22 b 2 > 33 c 3 > 44 d 4 > 55 e 5 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Dear Ann, To elaborate Spencer's answer slightly, R requires that the row names of a data frame be unique. That's why it generated different row names for the rows of the two data frames given as arguments to rbind. Again, the question is what you want to do here. You could generate more meaningful row names (e.g., "A1" - "A5", "B1" - "B5" for your original example) if this seems appropriate, but they have to be unique. John At 09:34 AM 4/21/2003 -0700, Spencer Graves wrote:> > df1 <- data.frame(a=1:2) > > df2 <- rbind(df1, df1) > > df2 > a >1 1 >2 2 >11 1 >22 2 > > rownames(df2) <- 1:4 > >hope this helps. spencer graves > >Ann Hess wrote: >>I am using rbind to concatenate two dataframes, but after the rbind I get >>a strange "double" index. >>I am using rbind a number of times to make one large dataset. Is there a >>way to prevent the "double" index? Or is there some other way to >>concatenate the data (it looks like merge only works by columns)? >>Here is an example of the "double" index. >> >>>numbers<-(1:5) >>>letters<-matrix(c("a","b","c","d","e"),5,1) >>>data<-data.frame(letters,numbers) >>>doubledata<-rbind(data,data) >>>doubledata >> letters numbers >>1 a 1 >>2 b 2 >>3 c 3 >>4 d 4 >>5 e 5 >>11 a 1 >>22 b 2 >>33 c 3 >>44 d 4 >>55 e 5 >>______________________________________________ >>R-help at stat.math.ethz.ch mailing list >>https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox