Purna chander
2012-Oct-25 06:24 UTC
[R] problem in finding sizes of objects using a for loop
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
Jorge I Velez
2012-Oct-25 06:40 UTC
[R] problem in finding sizes of objects using a for loop
Dear Purna, You need the get() function around object[i] in order to accomplish the same results: # data x<-rnorm(10000) y<-runif(100,min=40,max=1000) # sizes objects<-ls() for (i in seq_along(objects)){ print(c(objects[i],object.size(get(objects[i])))) # get() is added here } [1] "x" "80040" [1] "y" "840" get() is needed because each element of "objects" is a character and the object.size() function does not operates on characters, but on objects (not your variable, the definition in R). See ?get and ?object.size for more information. HTH, Jorge.- On Thu, Oct 25, 2012 at 5:24 PM, Purna chander <> 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@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]]
Hi, ?You can also use lapply() or sapply() without the get() function. ?list1<-list(x=x,y=y) ?sapply(list1,object.size) #??? x???? y #80040?? 840 ?do.call(rbind,lapply(list1,object.size)) ? # [,1] #x 80040 #y?? 840 A.K. ----- Original Message ----- From: Jorge I Velez <jorgeivanvelez at gmail.com> To: Purna chander <chanderbio at gmail.com> Cc: r-help <r-help at r-project.org> Sent: Thursday, October 25, 2012 2:40 AM Subject: Re: [R] problem in finding sizes of objects using a for loop Dear Purna, You need the get() function around object[i] in order to accomplish the same results: # data x<-rnorm(10000) y<-runif(100,min=40,max=1000) # sizes objects<-ls() for (i in seq_along(objects)){ ? print(c(objects[i],object.size(get(objects[i]))))? # get() is added here } [1] "x"? ? "80040" [1] "y"? "840" get() is needed because each element of "objects" is a character and the object.size() function does not operates on characters, but on objects (not your variable, the definition in R).? See ?get and ?object.size for more information. HTH, Jorge.- On Thu, Oct 25, 2012 at 5:24 PM, Purna chander <> 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. >??? [[alternative HTML version deleted]] ______________________________________________ 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
2012-Oct-25 17:56 UTC
[R] problem in finding sizes of objects using a for loop
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 ------- 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.