简体   繁体   中英

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 . I would like to be able to rbind them but it is costly to write out:

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.

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.

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)))

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