How can I access all objects of class data.frame inside .GlobalEnv in R

后端 未结 4 861
醉话见心
醉话见心 2021-01-25 03:12

I have 8,000 data.frames inside my global environment (.GlobalEnv) in R, for example

head(ls(.GlobalEnv))
#[1] \"db1\" \"db2\" \"db3\"          


        
4条回答
  •  忘掉有多难
    2021-01-25 03:45

    The simplest approach I can think of would be a basic for loop using mget.

    for(df in ls(.GlobalEnv)){
        print(get(df))
    }
    

    You can then apply whatever operation you like on the mget result.

    Note - this assumes the only variables in the environment are data.frames for your purposes as it doesn't discriminate A more restrictive for loop would be:

    for(df in ls(.GlobalEnv)){
        if(is.data.frame(get(df))){
            print(head(get(df)))
        }
    }
    

    which just uses is.data.frame to check if the object is indeed a data.frame.

提交回复
热议问题