> -----Original Message----- > From: David L Carlson [mailto:dcarl...@tamu.edu] > Sent: Friday, November 14, 2014 10:25 AM > To: John Posner; 'r-help@r-project.org' > Subject: RE: Help with ddply/summarize > > I think this is what you want: > > > MyVar <- 1:10 > > MyVar > [1] 1 2 3 4 5 6 7 8 9 10 > > mean(MyVar) > [1] 5.5 > > txt <- "MyVar" > > mean(txt) > [1] NA > Warning message: > In mean.default(txt) : argument is not numeric or logical: returning NA > > mean(get(txt)) > [1] 5.5 >
Got it. I was also pointed to get() by David Winsemius. Thanks to both Davids! -John > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > > > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On Behalf Of John Posner > Sent: Thursday, November 13, 2014 5:32 PM > To: 'r-help@r-project.org' > Subject: [R] Help with ddply/summarize > > I have a straightforward application of ddply() and summarize(): > > ddply(MyFrame, .(Treatment, Week), summarize, > MeanValue=mean(MyVar)) > > This works just fine: > > Treatment Week MeanValue > 1 MyDrug BASELINE 5.91 > 2 MyDrug WEEK 1 4.68 > 3 MyDrug WEEK 2 4.08 > 4 MyDrug WEEK 3 3.67 > 5 MyDrug WEEK 4 2.96 > 6 MyDrug WEEK 5 2.57 > 7 MyDrug WEEK 6 2.50 > 8 Placebo BASELINE 8.58 > 9 Placebo WEEK 1 8.25 > ... > > But I want to specify the variable (MyVar) as a character string: > > ddply(MyFrame, .(Treatment, Week), summarize, > MeanValue=mean("MyVar")) > > (Actually, the character string "MyVar" will be selected from a vector of > character strings.) > > The code above produces no joy: > > Treatment Week MeanValue > 1 MyDrug BASELINE NA > 2 MyDrug WEEK 1 NA > 3 MyDrug WEEK 2 NA > 4 MyDrug WEEK 3 NA > ... > I tried a few things, including: > > as.name("MyVar") > as.quoted("MyVar") > > ... but they all produced the name results: NAs > > I'm obviously thrashing around in the dark! Any advice would be greatly > appreciated. > > -John > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. ______________________________________________ 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.