带有占位符的R中的cbind数据框

cbind dataframe in R with placeholders

假设我有三个数据帧:

data.frame1 <- data.frame(x=c(1:10))
data.frame2 <- data.frame(x=c(11:20))
data.frame3 <- data.frame(x=c(21:30))

我可以通过明确命名它们中的每一个来将它们绑定在一起:

res.data.frame <- cbind(data.frame1, data.frame2, data.frame3)

但是,我正在寻找更动态的方式来做到这一点,例如带占位符。

这以某种方式将三个数据帧保存在一个新的数据帧中,但不是可用的格式:

res.data.frame1 <- as.data.frame(mapply(get, grep("^data.frame.$", ls(), value=T)))

这个命令只会保存三个名字:

res.data.frame2 <- grep(pattern = "^data.frame.$", ls(), value=T)

这个只给出错误信息:

res.data.frame3  <- do.call(cbind, lapply(ls(pattern = "^data.frame.$")), get)

有谁知道这样做的正确方法吗?

也许是这样的?

假设ls()

# [1] "data.frame1" "data.frame2" "data.frame3"

as.data.frame(Reduce("cbind", sapply(ls(), function(i) get(i))))

根据@akrun 的评论,这可以简化为

as.data.frame(Reduce("cbind", mget(ls())))