Re: [R] dimensions of a all objects
Generally it is difficult to get an overview of what's there. But the following function I acquired from (???) ages ago does a nice job: lls - function (pos = 1, pat = ) { dimx - function(dd) if (is.null(dim(dd))) length(dd) else dim(dd) lll - ls(pos = pos, pat = pat) cat(formatC(mode, 1, 15), formatC(class, 1, 18), formatC(name, 1, max(nchar(lll)) + 1), size\n---\n) if (length(lll) 0) { for (i in 1:length(lll)) { cat(formatC(eval(parse(t = paste(mode(, lll[i], , 1, 15), formatC(paste(eval(parse(t = paste(class(, lll[i], , collapse = ), 1, 18), formatC(lll[i], 1, max(nchar(lll)) + 1), , eval(parse(t = paste(dimx(, lll[i], , \n) } } } Just say lls() and you get a reasnoable listing of obejcts. Best, Bendix __ Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2-4 DK-2820 Gentofte Denmark +45 44 43 87 38 (direct) +45 30 75 87 38 (mobile) +45 44 43 73 13 (fax) [EMAIL PROTECTED] http://www.biostat.ku.dk/~bxc __ -Original Message- From: Farrel Buchinsky [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 09, 2007 3:30 PM To: r-help@stat.math.ethz.ch Subject: [R] dimensions of a all objects Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) -- Farrel Buchinsky [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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] dimensions of a all objects
Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) -- Farrel Buchinsky [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] dimensions of a all objects
You need something like this: sapply(objects() , function(x)(dim(eval(parse(text = x) a - rnorm(1) b - matrix(rnorm(4), ncol = 2, nrow = 2) sapply(objects() , function(x)(dim(eval(parse(text = x) $a NULL $b [1] 2 2 Cheers, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -Oorspronkelijk bericht- Van: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Namens Farrel Buchinsky Verzonden: dinsdag 9 januari 2007 15:30 Aan: r-help@stat.math.ethz.ch Onderwerp: [R] dimensions of a all objects Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) -- Farrel Buchinsky [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] dimensions of a all objects
Farrel Buchinsky wrote: Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) 'objects()' returns character strings - the names of objects - then the dim of the character strings are all NULL. I'll assume that's what you are getting at - you've not posted an example or the output you are getting or why it 'does not work'. Maybe you want this: sapply(objects(),function(x){dim(get(x))}) $f NULL $m [1] 2 5 $x NULL $y [1] 5 2 - where m and y are matrices, f is a function, x is a scalar. Barry __ R-help@stat.math.ethz.ch 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.
Re: [R] dimensions of a all objects
BaRow == Barry Rowlingson [EMAIL PROTECTED] on Tue, 09 Jan 2007 14:53:05 + writes: BaRow Farrel Buchinsky wrote: Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) BaRow 'objects()' returns character strings - the names of objects - then BaRow the dim of the character strings are all NULL. BaRow I'll assume that's what you are getting at - you've not posted an BaRow example or the output you are getting or why it 'does not work'. BaRow Maybe you want this: sapply(objects(),function(x){dim(get(x))}) BaRow $f BaRow NULL BaRow $m BaRow [1] 2 5 BaRow $x BaRow NULL BaRow $y BaRow [1] 5 2 BaRow - where m and y are matrices, f is a function, x is a scalar. Yes. Since he's just interested in printing, maybe ls.str() # would be even more revealing (or maybe too # confusing for a newbie) Martin __ R-help@stat.math.ethz.ch 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.
Re: [R] dimensions of a all objects
You have assumed everything as I meant it. I understand what is happening now. ls() simply creates a vector with character elements and dim() sees each element as not having dimensions. The critical part of what you have shown is the get(command) that turns what is just a string into the dataframe or vector whose name is the string. The other issue which you showed, and one that I have come across before is that sapply and tapply and lapply cannot handle a function on a function. I would have thought that I should get the same result from lapply(ls(),dim(get())) or something such as that. But instead one has to create a function command within the lapply to handle a dimension command upon a get command. On 1/9/07, Barry Rowlingson [EMAIL PROTECTED] wrote: Farrel Buchinsky wrote: Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) 'objects()' returns character strings - the names of objects - then the dim of the character strings are all NULL. I'll assume that's what you are getting at - you've not posted an example or the output you are getting or why it 'does not work'. Maybe you want this: sapply(objects(),function(x){dim(get(x))}) $f NULL $m [1] 2 5 $x NULL $y [1] 5 2 - where m and y are matrices, f is a function, x is a scalar. Barry -- Farrel Buchinsky Mobile: (412) 779-1073 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] dimensions of a all objects
See below. On 1/9/07, Farrel Buchinsky [EMAIL PROTECTED] wrote: You have assumed everything as I meant it. I understand what is happening now. ls() simply creates a vector with character elements and dim() sees each element as not having dimensions. The critical part of what you have shown is the get(command) that turns what is just a string into the dataframe or vector whose name is the string. The other issue which you showed, and one that I have come across before is that sapply and tapply and lapply cannot handle a function on a function. I would have thought that I should get the same result from lapply(ls(),dim(get())) or something such as that. The gsubfn package can do nearly that. Just preface the function of interest (in this case sapply) with fn$ and then you can write the function as a formula: library(gsubfn) fn$sapply(c(iris, CO2), ~ dim(get(x)), simplify = FALSE) $iris [1] 150 5 $CO2 [1] 84 5 But instead one has to create a function command within the lapply to handle a dimension command upon a get command. On 1/9/07, Barry Rowlingson [EMAIL PROTECTED] wrote: Farrel Buchinsky wrote: Why will the following command not work sapply(objects(),dim) What does it say about the objects list? What does it say about the dim command? Likewise, the following also does not work all-ls() for (f in all) print(dim(f)) 'objects()' returns character strings - the names of objects - then the dim of the character strings are all NULL. I'll assume that's what you are getting at - you've not posted an example or the output you are getting or why it 'does not work'. Maybe you want this: sapply(objects(),function(x){dim(get(x))}) $f NULL $m [1] 2 5 $x NULL $y [1] 5 2 - where m and y are matrices, f is a function, x is a scalar. Barry -- Farrel Buchinsky Mobile: (412) 779-1073 [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.