Skip to content
Prev 333574 / 398525 Next

averaging rows on a data.frame according to a factor

On Nov 22, 2013, at 1:43 PM, john d wrote:

            
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.
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)
Newbie or not, you should still read the Posting Guide.