[R] problem in finding sizes of objects using a for loop

PIKAL Petr petr.pikal at precheza.cz
Fri Oct 26 11:14:15 CEST 2012


Hi

I am not sure where I get it from, but this one gives some more info then size

ls.objects
function (pos = 1, pattern, order.by) 
{
    napply <- function(names, fn) sapply(names, function(x) fn(get(x, 
        pos = pos)))
    names <- ls(pos = pos, pattern = pattern)
    obj.class <- napply(names, function(x) as.character(class(x))[1])
    obj.mode <- napply(names, mode)
    obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
    obj.size <- napply(names, object.size)
    obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2]))
    vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
    obj.dim[vec, 1] <- napply(names, length)[vec]
    out <- data.frame(obj.type, obj.size, obj.dim)
    names(out) <- c("Type", "Size", "Rows", "Columns")
    if (!missing(order.by)) 
        out <- out[order(out[[order.by]]), ]
    out
}

> ls.objects()
                      Type   Size Rows Columns
a_vec              numeric    824  100      NA
aaglo           data.frame   3944   50       7
ad              data.frame   2072   24       4
airquality      data.frame   5024  153       6
alice           data.frame   7584   14      33
alp             data.frame   6080   35      12
alp.v       nfnGroupedData   6136   20      12

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of David Winsemius
> Sent: Thursday, October 25, 2012 8:20 PM
> To: jim holtman
> Cc: r-help
> Subject: Re: [R] problem in finding sizes of objects using a for loop
> 
> 
> On Oct 25, 2012, at 10:56 AM, jim holtman wrote:
> 
> > Here is a function I use to get the size of objects:
> >
> > Here is an example output:
> >
> >> my.ls()
> >                     Size     Mode
> > allStores       7,303,224     list
> > convertedStores         0     NULL
> > f.createCluster    40,508 function
> > x                  41,672     list
> > **Total         7,385,404  -------
> 
> That's far more elegant that the one I use;
> 
> getsizes <-  function() {z <- sapply(ls(envir=globalenv()),
>                                 function(x) object.size(get(x)))
>                (tmp <- as.matrix(rev(sort(z))[1:10]))}
> getsizes()
> 
> Only returns the sorted-by-size matrix of the largest ten objects, but
> modifying it to return all of them should be trivial.
> 
> --
> david.
> 
> 
> >
> >
> > 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 (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$Mode <- c(unlist(lapply(rownames(.ls)[-nrow(.ls)], function(x)
> > mode(eval(as.symbol(x),
> >        envir = envir)))), "-------")
> >    .ls
> > }
> >
> >
> > On Thu, Oct 25, 2012 at 2:24 AM, Purna chander <chanderbio at gmail.com>
> wrote:
> >> Dear All,
> >>
> >> I wanted to extract the sizes of all created objects. For E.g when I
> >> created 2 objects(x and y), I got their sizes using the following
> >> code:
> >>
> >>> x<-rnorm(10000)
> >>> y<-runif(100,min=40,max=1000)
> >>> ls()
> >> [1] "x" "y"
> >>> object.size(x)
> >> 80024 bytes
> >>> object.size(y)
> >> 824 bytes
> >>
> >> However, I was unable to get their sizes when I used a for loop in
> the
> >> following way:
> >>
> >>> objects<-ls()
> >>> for (i in seq_along(objects)){
> >> +   print(c(objects[i],object.size(objects[i])))
> >> +
> >> + }
> >> [1] "x"  "64"
> >> [1] "y"  "64"
> >>
> >>
> >> The result obtained by me is wrong in second case.
> >>
> >> I understood that variables x and y are treated as characters. But
> to
> >> rectify this problem.
> >>
> >> Regards,
> >> Purna
> >>
> >> ______________________________________________
> >> 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.
> >
> >
> >
> > --
> > 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.
> >
> > ______________________________________________
> > 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.
> 
> David Winsemius, MD
> Alameda, CA, USA
> 
> ______________________________________________
> 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