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]]
On Nov 13, 2014, at 3:31 PM, John Posner wrote:> 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: NAsThe data example from ddply's help page: dfx <- data.frame( group = c(rep('A', 8), rep('B', 15), rep('C', 6)), sex = sample(c("M", "F"), size = 29, replace = TRUE), age = runif(n = 29, min = 18, max = 54) ) ddply(dfx, .(group, sex), summarize, mean = round(mean( get('age') ), 2)) group sex mean 1 A F 34.81 2 A M 33.38 3 B F 35.89 4 B M 33.67 5 C F 33.38 6 C M 35.36 Group - sex ... a very `60's sort of result. -- David Winsemius Alameda, CA, USA
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 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of John Posner Sent: Thursday, November 13, 2014 5:32 PM To: 'r-help at 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 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.