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]]
David Winsemius
2013-Nov-22 23:01 UTC
[R] averaging rows on a data.frame according to a factor
On Nov 22, 2013, at 1:43 PM, john d wrote:> 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:Well, using the data,frame(cbind(...)) will ensure that you will NOT get factors, since cbind creates a matrix and that structure will not hold factors.> > res<-as.data.frame(cbind(c("a","b"), c(2,2.5),c(4,4)))Ouch. Look at ?tapply or ?aggregate. Perhaps: dat<-data.frame(facts= c("a","a","a","b","b"), nums1=c(1,2,3,3,2),nums2=c(4,3,5,4,4)) aggregate(dat[-1], dat[1], mean)> Any help would be greatly appreciated. > > Jonathan > > [[alternative HTML version deleted]]Newbie or not, you should still read the Posting Guide.> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html-- David Winsemius Alameda, CA, USA
Rui Barradas
2013-Nov-22 23:02 UTC
[R] averaging rows on a data.frame according to a factor
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 at 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. >
Hi,
dat<-as.data.frame(cbind(c("a","a","a","b","b"),
c(1,2,3,3,2),c(4,3,5,4,4)))
?str(dat)
#'data.frame':??? 5 obs. of? 3 variables:
# $ V1: Factor w/ 2 levels "a","b": 1 1 1 2 2
# $ V2: Factor w/ 3 levels "1","2","3": 1 2 3 3 2
# $ V3: Factor w/ 3 levels "3","4","5": 2 1 3 2 2
#better way would be
dat<-data.frame(V1=c("a","a","a","b","b"),
V2=c(1,2,3,3,2),V3=c(4,3,5,4,4))
?str(dat)
#'data.frame':??? 5 obs. of? 3 variables:
# $ V1: Factor w/ 2 levels "a","b": 1 1 1 2 2
# $ V2: num? 1 2 3 3 2
# $ V3: num? 4 3 5 4 4
library(plyr)
?ddply(dat,.(V1),colwise(mean))
#or
?aggregate(.~V1,data=dat,mean)
A.K.
On Friday, November 22, 2013 5:45 PM, john d <dobzhanski at gmail.com>
wrote:
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 at 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.