Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R evaluate string as data frame

Tags:

dataframe

r

How can I evaluate a string of class character as data frame?

Concretely, I have several data frames let's say: x0,x1,x3:

x0 <- data.frame(a=1,b="a")
x1 <- data.frame(a=2,b="b")
x2 <- data.frame(a=3,b="c")

They have all the same structure and I would like to merge them with rbind. To avoid to call each single data frame I use regular expression:

x <- grep("x\\d",ls(),perl=TRUE,value=TRUE) 

This gives me a vector of class character. Now, I would like to merge them to one dataframe called x.all:

x.all <- rbind(x)

What I get is a matrix with dimension (1,3). Does anyone can give me a hint? Thanks very much for help.

like image 534
giordano Avatar asked Mar 18 '12 13:03

giordano


1 Answers

Using get and do.call:

do.call(rbind, lapply(x, get))
#   a b
# 1 1 a
# 2 2 b
# 3 3 c
like image 179
flodel Avatar answered Nov 14 '22 04:11

flodel