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

Matthew mccormack at molbio.mgh.harvard.edu
Wed Aug 13 21:32:19 CEST 2014


Jim, Wow that was cooool !   This function is *really* useful.  Thank 
you very much !  (It is also way beyond my capability).

I need to make a list of data frames because then I am going to bind 
them with plyr using 'dplyr::rbind_all(listOfDataFrames)'. This will 
make a single data frame, and from that single data frame I can make a 
heat map of all the data.

   For example, when I use your fantastic function, my.ls(), I get:

my.ls()
                                                              Size      Class  Length      Dim
.Random.seed                                                2,544    integer     626
cpl                                                        28,664  character     512
filenames                                                   2,120  character      19
filepath                                                      216  character       1
i                                                             152  character       1
Mer7_1-1_160-226A_1_gene_exp_diff_filt_hc_log2.txt         81,152 data.frame       3  529 x 3
Mer7_1-1_Mer7_1-2_gene_exp_diff_filt_hc_log2.txt           31,624 data.frame       3  199 x 3
Mer7_1-1_S150-160-226A_1_gene_exp_diff_filt_hc_log2.txt    81,152 data.frame       3  529 x 3
Mer7_1-1_W29_1_gene_exp_diff_filt_hc_log2.txt             129,376 data.frame       3  849 x 3
Mer7_1-1_W29_S150-226A_1_gene_exp_diff_filt_hc_log2.txt   126,816 data.frame       3  835 x 3
Mer7_1-1_W29_S160-162A_1_gene_exp_diff_filt_hc_log2.txt    82,792 data.frame       3  537 x 3
Mer7_1-1_W29_S226A_1_gene_exp_diff_filt_hc_log2.txt       115,008 data.frame       3  756 x 3
Mer7_1-2_160-226A_1_gene_exp_diff_filt_hc_log2.txt         79,936 data.frame       3  519 x 3
Mer7_1-2_S150-160-226A_1_gene_exp_diff_filt_hc_log2.txt    84,512 data.frame       3  548 x 3
Mer7_1-2_W29_1_gene_exp_diff_filt_hc_log2.txt             130,568 data.frame       3  857 x 3
Mer7_1-2_W29_S160-162A_1_gene_exp_diff_filt_hc_log2.txt    83,768 data.frame       3  542 x 3
Mer7_1-2_W29_S226A_1_gene_exp_diff_filt_hc_log2.txt       119,008 data.frame       3  783 x 3
Mer7_2-1_160-226A_2_gene_exp_diff_filt_hc_log2.txt        105,344 data.frame       3  685 x 3
Mer7_2-1_Mer7_2-2_gene_exp_diff_filt_hc_log2.txt           26,216 data.frame       3  166 x 3
Mer7_2-1_S150-160-226A_2_gene_exp_diff_filt_hc_log2.txt   106,368 data.frame       3  693 x 3
Mer7_2-1_W29_2_gene_exp_diff_filt_hc_log2.txt             160,200 data.frame       3 1053 x 3
Mer7_2-1_W29_S150-226A_2_gene_exp_diff_filt_hc_log2.txt   152,696 data.frame       3 1005 x 3
Mer7_2-1_W29_S160-162A_2_gene_exp_diff_filt_hc_log2.txt   113,992 data.frame       3  743 x 3
Mer7_2-1_W29_S226A_2_gene_exp_diff_filt_hc_log2.txt       138,944 data.frame       3  914 x 3
my.ls                                                      35,624   function       1
myfiles                                                     2,120  character      19
names                                                       2,424       list      19
test                                                          680  character       5
whatisthis                                                  2,424       list      19
**Total                                                 2,026,440    ------- -------  -------



   What I need is make the list of data frames for the dplyr command, 
dplyr::rbind_all(listOfDataFrames). Ideally, this would also be a 
specific subset of all the data frames, say the data frames with W29 in 
the name. This is something we, our lab, would be doing routinely and at 
various times of the day, so I want to automate the process so it does 
not need anyone to manually sit at the computer and type the list of 
data frames.

Matthew


On 8/13/2014 3:06 PM, jim holtman wrote:
> Here is a function that I use that might give you the results you want:
>
> =================
>> my.ls()
>                         Size      Class  Length         Dim
> .Random.seed          2,544    integer     626
> .remapHeaderFile     40,440 data.frame       2     373 x 2
> colID                   216  character       3
> delDate                 104  character       1
> deliv                15,752 data.table       7     164 x 7
> f_drawPallet         36,896   function       1
> i                        96  character       1
> indx                168,816  character    1782
> pallet              172,696 data.table       3    1782 x 3
> pallets             405,736 data.table      14   1782 x 14
> picks            26,572,856 data.table      19 154247 x 19
> wb                      656   Workbook       1
> wSplit           68,043,136       list    1782
> x                        56    numeric       2
> **Total          95,460,000    ------- -------     -------
>
> ====================
>> my.ls
> function (pos = 1, sorted = FALSE, envir = as.environment(pos))
> {
>      .result <- sapply(ls(envir = envir, all.names = TRUE),
> function(..x) object.size(eval(as.symbol(..x),
>          envir = envir)))
>      if (length(.result) == 0)
>          return("No objects to list")
>      if (sorted) {
>          .result <- rev(sort(.result))
>      }
>      .ls <- as.data.frame(rbind(as.matrix(.result), `**Total` = sum(.result)))
>      names(.ls) <- "Size"
>      .ls$Size <- formatC(.ls$Size, big.mark = ",", digits = 0,
>          format = "f")
>      .ls$Class <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)],
> function(x) class(eval(as.symbol(x),
>          envir = envir))[1L])), "-------")
>      .ls$Length <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)],
>          function(x) length(eval(as.symbol(x), envir = envir)))),
>          "-------")
>      .ls$Dim <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)], function(x)
> paste(dim(eval(as.symbol(x),
>          envir = envir)), collapse = " x "))), "-------")
>      .ls
> }
> ========================
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
>
> 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.


	[[alternative HTML version deleted]]



More information about the R-help mailing list