How can I make a list of all dataframes that are in my global environment?

后端 未结 6 2317
不知归路
不知归路 2020-11-28 14:02

I am trying to use rbind on them. But I need a list of all the dataframes that are already in my global environment. How can I do it?

Code

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 14:22

    This function should return a proper list with all the data.frames as elements

    dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))
    

    then you can rbind them with

    do.call(rbind, dfs)
    

    Of course it's awfully silly to have a bunch of data.frames lying around that are so related that you want to rbind them. It sounds like they probably should have been in a list in the first place.

    I recommend you say away from assign(), that's always a sign things are probably afoul. Try

    temp <- list.files(pattern="*.csv")
    dfs <- lapply(temp, read.csv)
    

    that should return a list straight away.

提交回复
热议问题