简体   繁体   English

如何在R中创建向量名称字符串,然后可以对其进行求值?

[英]How to create a string of vector names in R that will then be able to be evaluated?

In R, I currently have 100 dataframes, named df.1 , ..., df.100 . 在R中,我目前有100个数据帧,分别名为df.1 ,..., df.100 I would like to be able to rbind them but it is costly to write out: 我希望能够rbind它们,但是写出来的代价rbind

rbind(df.1, df.2, etc)

So, I have tried: 因此,我尝试了:

rbind(eval(as.symbol(paste0("df.",1:84, collapse = ", "))))

However, this returns errors. 但是,这将返回错误。 Does anyone know how I can make the dataframes usable? 有谁知道我如何使数据框可用? thanks. 谢谢。

You can rbind them one at a time in a loop. 您可以一次循环rbind一次。

df.1 = iris
df.2 = iris
df.3 = iris

DF = df.1
for(i in 2:3) {
    DF = rbind(DF, eval(as.symbol(paste("df", i, sep=".")))) }

Using mget and then do.call or dplyr 's bind_rows should work. 使用mget然后执行do.calldplyrbind_rows应该可以。

df.1 = iris[1:20,]
df.2 = iris[21:50,]

do.call("rbind",mget(paste0("df.",1:2)))

library(dplyr)
bind_rows(mget(paste0("df.",1:2)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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