Use object names as list names in R

前端 未结 2 1824
失恋的感觉
失恋的感觉 2020-12-19 04:10

Of course I could name the objects in my list all manually like this:

    #create dfs
    df1<-data.frame(a=sample(1:50,10),b=sample(1:50,10),c=sample(1:5         


        
2条回答
  •  轮回少年
    2020-12-19 04:29

    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:]]+$")
    

提交回复
热议问题