Thanks for the help everyone! I'm new to vectors, and don't quite get it.
 This works for me:

binary.vars <- c("q1", "q2", "q3", ...)

apply(mydata[binary.vars], 2, tapply, mydata["male"], mean)

but this doesn't:

other.vars <- c("male", "race", "religion")

apply(mydata[other.binary.vars], 2, tapply, mydata[other.vars], mean)

What am I missing?


On Tue, Apr 7, 2009 at 6:14 PM, hadley wickham <h.wickham@gmail.com> wrote:

> On Tue, Apr 7, 2009 at 4:41 PM, Jorge Ivan Velez
> <jorgeivanvelez@gmail.com> wrote:
> > Hi Eik,
> > You're absolutely right. My bad.
> >
> > Here is the correction of the code I sent:
> >
> > apply(mydata[,-1], 2, tapply, mydata[,1], function(x) sum(x)/length(x))
>
> Or more simply:
>
> apply(mydata[,-1], 2, tapply, mydata[,1], mean)
>
> Hadley
>
> --
> http://had.co.nz/
>

	[[alternative HTML version deleted]]


