Tony
Tony

Reputation: 2929

How to add variables to a dataframe using paste?

I have a set of R objects named c1,c2,c3,...,c10 each with same length and like to form a dataframe from these objects. I could do it by df<-data.frame(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10). Is there a more efficient way of doing this, perhaps using paste?

Thanks for your help.

Upvotes: 1

Views: 692

Answers (1)

James
James

Reputation: 66844

data.frame(sapply(paste("c",1:10,sep=""),get))

or,

data.frame(mget(paste("c",1:10,sep=""),.GlobalEnv))

Upvotes: 5

Related Questions