Use object names as list names in R

前端 未结 2 1826
失恋的感觉
失恋的感觉 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:30

    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

提交回复
热议问题