[R] pasting columns of a dataframe together

Anika Masters anika.masters at gmail.com
Thu Jun 27 00:14:11 CEST 2013


I want  to "paste" the contents of the first column of a dataframe
with all the columns of the same dataframe.



What is a "good" way to do this, perhaps something more elegant than
what I have below.  (My actual dataframe has ~400 columns and ~35000
rows.)



example1:

mydf <- data.frame( matrix(data=1:15, nrow=5, ncol=3) )

mydf2 <- mydf

temp2 <- paste(unlist(mydf[,1]) , unlist(mydf), sep="_")

mydf2[ ,] <- temp2



example2:

mydf2 <- mydf



for(i in 1:ncol(mydf2) ) {

mydf2[i] <- paste( mydf[,1], mydf[ ,i], sep='_')

}



More information about the R-help mailing list