Use object names as list names in R

左心房为你撑大大i 提交于 2019-11-29 10:11:09

Find the names, then call mget.
If there is a pattern to the names of each individual variable, then this is straightforward.

var_names <- paste0("df", 1:3)
mget(var_names, envir = globalenv())  #or maybe envir = parent.frame()

If the naming system is more complicated, you can use regular expressions to find them, using something like

var_names <- ls(envir = globalenv(), pattern = "^df[[:digit:]]+$")

If you just want to name a list with names from the environment that share something, in this case 'df':

names(list.1) <- grep("df",ls(),value=TRUE)

If you want to push your environment into a list:

list.1 <- globalenv()
list.1 <- as.list(list.1) 

To reverse the process see ?list2env

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!