compute means of a group by factor

前端 未结 4 620
太阳男子
太阳男子 2021-01-02 06:49

Is there a way that this can be improved, or done more simply?

means.by<-function(data,INDEX){
  b<-by(data,INDEX,function(d)apply(d,2,mean))
  return(         


        
4条回答
  •  悲&欢浪女
    2021-01-02 07:24

    You want tapply or ave, depending on how you want your output:

    > Data <- data.frame(grp=sample(letters[1:3],20,TRUE),x=rnorm(20))
    > ave(Data$x, Data$grp)
     [1] -0.3258590 -0.5009832 -0.5009832 -0.2136670 -0.3258590 -0.5009832
     [7] -0.3258590 -0.2136670 -0.3258590 -0.2136670 -0.3258590 -0.3258590
    [13] -0.3258590 -0.5009832 -0.2136670 -0.5009832 -0.3258590 -0.2136670
    [19] -0.5009832 -0.2136670
    > tapply(Data$x, Data$grp, mean)
             a          b          c 
    -0.5009832 -0.2136670 -0.3258590 
    
    # Example with more than one column:
    > Data <- data.frame(grp=sample(letters[1:3],20,TRUE),x=rnorm(20),y=runif(20))
    > do.call(rbind,lapply(split(Data[,-1], Data[,1]), mean))
                 x         y
    a -0.675195494 0.4772696
    b  0.270891403 0.5091359
    c  0.002756666 0.4053922
    

提交回复
热议问题