[R] Help with ddply/summarize

John Posner john.posner at MJBIOSTAT.COM
Fri Nov 14 00:31:53 CET 2014


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]]



More information about the R-help mailing list