[R] pasting columns of a dataframe together
Anika Masters
anika.masters at gmail.com
Thu Jun 27 00:21:50 CEST 2013
Given the size of my dataframe (>400 columns, >35000 rows), I should
have clarified that speed and efficiency is perhaps as important as
"elegance", and I'd like to use the examples to learn about any
corrections and improvements I can make in the examples I provided.
Thanks for any help you can offer. (I am using R x64 2.15.2 on Windows
7.)
On Wed, Jun 26, 2013 at 3:14 PM, Anika Masters <anika.masters at gmail.com> wrote:
> 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