[R] find the data frames in list of objects and make a list of them

William Dunlap wdunlap at tibco.com
Thu Aug 14 01:40:32 CEST 2014


Previously you asked
>     A second question: is this the best way to make a list
>    of data frames without having to manually type c(dataframe1, dataframe2, ...)  ?

If you use 'c' there you will not get a list of data.frames - you will
get a list of all the columns in the data.frame you supplied.  Use
'list' instead of 'c' if you are taking that route.

The *apply functions are helpful  here.  To make list of all
data.frames in an environment you can use the following function,
which takes the environment to search as an argument.

f <- function(envir = globalenv()) {
    tmp <- eapply(envir,
                           all.names=TRUE,
                           FUN=function(obj) if (is.data.frame(obj))
obj else NULL)
    # remove NULL's now
    tmp[!vapply(tmp, is.null, TRUE)]
}

Use is as
  allDataFrames <- f(globalenv()) # or just f()






Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Aug 13, 2014 at 3:49 PM, Matthew
<mccormack at molbio.mgh.harvard.edu> wrote:
> Hi Richard,
>
>     Thank you very much for your reply and your code.
> Your code is doing just what I asked for, but does not seem to be what I
> need.
>
> I will need to review some basic R before I can continue.
>
> I am trying to list data frames in order to bind them into 1 single data
> frame with something like: dplyr::rbind_all(list of data frames), but when I
> try dplyr::rbind_all(lsDataFrame(ls())), I get the error: object at index 1
> not a data.frame. So, I am going to have to learn some more about lists in R
> before proceding.
>
> Thank you for your help and code.
>
> Matthew
>
>
>
>
>
> Matthew
>
> On 8/13/2014 3:12 PM, Richard M. Heiberger wrote:
>>
>> I would do something like this
>>
>> lsDataFrame <- function(xx=ls()) xx[sapply(xx, function(x)
>> is.data.frame(get(x)))]
>> ls("package:datasets")
>> lsDataFrame(ls("package:datasets"))
>>
>> On Wed, Aug 13, 2014 at 2:56 PM, Matthew
>> <mccormack at molbio.mgh.harvard.edu> wrote:
>>>
>>> Hi everyone,
>>>
>>>     I would like the find which objects are data frames in all the
>>> objects I
>>> have created ( in other words in what you get when you type: ls()  ),
>>> then I
>>> would like to make a list of these data frames.
>>>
>>> Explained in other words; after typing ls(), you get the names of
>>> objects.
>>> Which objects are data frames ?  How to then make a list of these data
>>> frames.
>>>
>>>     A second question: is this the best way to make a list of data frames
>>> without having to manually type c(dataframe1, dataframe2, ...)  ?
>>>
>>> Matthew
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list