简体   繁体   中英

R evaluate string as data frame

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.

Using get and do.call :

do.call(rbind, lapply(x, get))
#   a b
# 1 1 a
# 2 2 b
# 3 3 c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM