[R] Help with ddply/summarize
John Posner
john.posner at MJBIOSTAT.COM
Fri Nov 14 17:19:45 CET 2014
> -----Original Message-----
> From: David L Carlson [mailto:dcarlson at tamu.edu]
> Sent: Friday, November 14, 2014 10:25 AM
> To: John Posner; 'r-help at 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-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.
More information about the R-help
mailing list