group by in R, ddply with weighted.mean

前端 未结 2 968
天涯浪人
天涯浪人 2020-12-05 08:37

I am trying to do a \"group by\" - style weighted mean in R. With some basic mean the following code (using the plyr package from Hadley) worked well.

ddply         


        
2条回答
  •  无人及你
    2020-12-05 09:07

    Use summarise (or summarize):

    ddply(iris, "Species", summarise, 
      wmn = weighted.mean(Sepal.Length, Petal.Length),
      mn = mean(Sepal.Length))
    

提交回复
热议问题