[R] Newbie: how to get unique x unique x aggregate chart?

Marc Schwartz marc_schwartz at comcast.net
Wed Nov 15 21:13:47 CET 2006


On Wed, 2006-11-15 at 15:03 -0500, Christian Convey wrote:
> Thanks, let me try to clarify my question with an example.
> 
> Suppose I have the following data:
> 
> Gender,   Major,  Course-Grade
> F, Psy, 3.5
> F, Psy, 3.1
> M, Hst, 3.7
> F,  Hst,  3.6
> M, Hst,  2.6
> M, Eng, 3.9
> 
> I want to compute a table like the following:
> 
> X-axis: Gender
> Y-axis: Major
> Cell(x,y) = mean course-grade
> 
> So for example, with the data above:
> 
>           F     M
> ------------------------
> Psy |   3.3    NA
> Hst |   3.6    3.15
> Eng |  NA    3.9
> 
> If I were doing this in SQL I'd do it with a cross-tab query.  But the world
> of R still has much unfamiliar terrain :)
> 
> Thanks,
> Christian


Presuming that DF is a data frame containing your data:

> with(DF, tapply(Course.Grade, list(Major, Gender), 
                  mean, na.rm = TRUE))
      F    M
Eng  NA 3.90
Hst 3.6 3.15
Psy 3.3   NA


See ?tapply and ?with

HTH,

Marc Schwartz



More information about the R-help mailing list