Sorry, there's a bug in my previous answer.

dat <- data.frame(c("a","a","a","b","b"), c(1,2,3,3,2), c(4,3,5,4,4))

aggregate(as.matrix(dat[,-1]) ~ dat[,1], FUN = mean)


Don't use as.data.frame(cbind(...)), everything becomes factors.

Rui Barradas

Em 22-11-2013 23:02, Rui Barradas escreveu:
Hello,

Try the following.

aggregate(cbind(V2, V3) ~ V1, dat, FUN = mean)


Hope this helps,

Rui Barradas

Em 22-11-2013 21:43, john d escreveu:
Dear all,

I apologize for the newbie question, but I'm stuck.

I have a data frame in the following form:

dat<-as.data.frame(cbind(c("a","a","a","b","b"),
c(1,2,3,3,2),c(4,3,5,4,4)))

I need a way to generate a new dataframe with the average for each
factor.
The result should look like:

res<-as.data.frame(cbind(c("a","b"), c(2,2.5),c(4,4)))

Any help would be greatly appreciated.

Jonathan

    [[alternative HTML version deleted]]

______________________________________________
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.


______________________________________________
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.

______________________________________________
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