[R] Help with ddply/summarize

David Winsemius dwinsemius at comcast.net
Fri Nov 14 01:42:50 CET 2014


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: NAs

The 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



More information about the R-help mailing list