Matthew
2014-Aug-13 18:56 UTC
[R] find the data frames in list of objects and make a list of them
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
jim holtman
2014-Aug-13 19:06 UTC
[R] find the data frames in list of objects and make a list of them
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.
Richard M. Heiberger
2014-Aug-13 19:12 UTC
[R] find the data frames in list of objects and make a list of them
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.