On Sep 21, 2010, at 6:52 PM, Simon Kiss wrote:

hello, can someone tell me how to generate the means for a data frame that looks like this? My data frame has many more variables, but I won't bother you with those; these are the one's that I'm interested in. Needless to say, z is the variable in which I'm interested. I'd like to find out the mean score of z for NDP managers, Conservative managers and Liberal managers and then for a few other configurations. Ive played around with aggregate, tapply and by, but I can't get it to work.
Cordially,
Simon Kiss
mydata=data.frame(cbind(x,y,z))

How about testing your code? That throws an error;

mydata$x=as.factor(sample(c("labourers", "salaried", "managers"), size=300, replace=TRUE)) mydata$y=as.factor(sample(c("NDP", "Green", "Liberal", "Conservative"), size=300, replace=TRUE))
mydata$z=as.numeric(sample(1:4, size=300, replace=TRUE))

Try instead:
 mydata <-data.frame(
x=as.factor(sample(c("labourers", "salaried", "managers"), size=300, replace=TRUE)), y=as.factor(sample(c("NDP", "Green", "Liberal", "Conservative"), size=300, replace=TRUE)), z=as.numeric(sample(1:4, size=300, replace=TRUE))
                    )
> str(mydata)
'data.frame':   300 obs. of  3 variables:
$ x: Factor w/ 3 levels "labourers","managers",..: 2 2 1 1 1 2 1 3 2 3 ...
 $ y: Factor w/ 4 levels "Conservative",..: 3 2 3 2 4 1 1 3 3 4 ...
 $ z: num  3 3 2 4 4 1 1 3 4 1 ...

Then with a valid object ...
> with( subset(mydata, x == "managers"),
                        tapply(z, y, mean) )
Conservative        Green      Liberal          NDP
    2.161290     2.333333     2.615385     2.333333


*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
223 Grand River Hall, 171 Colborne Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 519 761 7606

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to