Continuing my learning curve after 25_ years with using SAS. Want to pull the "Mean" forom the summary of something... test <- rnorm(1000,1.5,1.25) hold <- summary(test) names(hold) [1] "Min." "1st Qu." "Median" "Mean" "3rd Qu." "Max." OK, so "Mean" is in there. So, is there a short form answer for why hold$Mean throws an error, and hold["Mean"} returns the mean (as desired)? Silly question I know, but gotta start somewhere... Thanks...
The short answer is that hold isn't a list-like object, and $ only works with list-like objects (lists and data frames, mainly). You can get the full explanation (VERY full), at ?Extract or any of its aliases, like ?'$' or ?'[' Sarah On Fri, Mar 31, 2017 at 7:11 PM, Evan Cooch <evan.cooch at gmail.com> wrote:> Continuing my learning curve after 25_ years with using SAS. Want to pull > the "Mean" forom the summary of something... > > test <- rnorm(1000,1.5,1.25) > > hold <- summary(test) > > names(hold) > [1] "Min." "1st Qu." "Median" "Mean" "3rd Qu." "Max." > > OK, so "Mean" is in there. > So, is there a short form answer for why hold$Mean throws an error, and > hold["Mean"} returns the mean (as desired)? > > Silly question I know, but gotta start somewhere... > > Thanks... >-- Sarah Goslee http://www.functionaldiversity.org
This is your answer:> str(hold)Classes 'summaryDefault', 'table' Named num [1:6] -2.602 0.636 1.514 1.54 2.369 ... ..- attr(*, "names")= chr [1:6] "Min." "1st Qu." "Median" "Mean" ... hold is a table of named numbers, i.e. a vector with a names attribute. It is not a data.frame so it does not have column names. The error message sort of tells you this when it says hold is an atomic vector (i.e. not a list or a data frame which are built from other objects such as vectors). David Carlson Anthropology Department Texas A&M University ________________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of Evan Cooch <evan.cooch at gmail.com> Sent: Friday, March 31, 2017 6:11 PM To: r-help at r-project.org Subject: [R] pull stat out of summary Continuing my learning curve after 25_ years with using SAS. Want to pull the "Mean" forom the summary of something... test <- rnorm(1000,1.5,1.25) hold <- summary(test) names(hold) [1] "Min." "1st Qu." "Median" "Mean" "3rd Qu." "Max." OK, so "Mean" is in there. So, is there a short form answer for why hold$Mean throws an error, and hold["Mean"} returns the mean (as desired)? Silly question I know, but gotta start somewhere... Thanks... ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
?str tells you the structure of any object. *Learn to use it!* It may well be the that you *cannot* do what you describe. As you should know by now in your "learning curve", invoking> objat the console silently invokes the print method for obj, and what is printed may in fact be calculated on the fly in the print method and not stored in an object anywhere. ?print.summary.lm is such an example: p-values are calculated and printed, but not stored. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Mar 31, 2017 at 4:54 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> The short answer is that hold isn't a list-like object, and $ only > works with list-like objects (lists and data frames, mainly). > > You can get the full explanation (VERY full), at > ?Extract > or any of its aliases, like > ?'$' > or > ?'[' > > Sarah > > On Fri, Mar 31, 2017 at 7:11 PM, Evan Cooch <evan.cooch at gmail.com> wrote: >> Continuing my learning curve after 25_ years with using SAS. Want to pull >> the "Mean" forom the summary of something... >> >> test <- rnorm(1000,1.5,1.25) >> >> hold <- summary(test) >> >> names(hold) >> [1] "Min." "1st Qu." "Median" "Mean" "3rd Qu." "Max." >> >> OK, so "Mean" is in there. >> So, is there a short form answer for why hold$Mean throws an error, and >> hold["Mean"} returns the mean (as desired)? >> >> Silly question I know, but gotta start somewhere... >> >> Thanks... >> > > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
> On 01 Apr 2017, at 01:54 , Sarah Goslee <sarah.goslee at gmail.com> wrote: > > The short answer is that hold isn't a list-like object, and $ only > works with list-like objects (lists and data frames, mainly). > > You can get the full explanation (VERY full), at > ?Extract > or any of its aliases, like > ?'$' > or > ?'[' >...and the intermediate answer is: hold["Mean"] -pd> Sarah > > On Fri, Mar 31, 2017 at 7:11 PM, Evan Cooch <evan.cooch at gmail.com> wrote: >> Continuing my learning curve after 25_ years with using SAS. Want to pull >> the "Mean" forom the summary of something... >> >> test <- rnorm(1000,1.5,1.25) >> >> hold <- summary(test) >> >> names(hold) >> [1] "Min." "1st Qu." "Median" "Mean" "3rd Qu." "Max." >> >> OK, so "Mean" is in there. >> So, is there a short form answer for why hold$Mean throws an error, and >> hold["Mean"} returns the mean (as desired)? >> >> Silly question I know, but gotta start somewhere... >> >> Thanks... >> > > -- > Sarah Goslee > http://www.functionaldiversity.org > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com