Set column name ddply

十年热恋 提交于 2020-01-11 05:55:31

问题


How to set the column name of the summarized data in

library(plyr)
ddply(data,.(col1,col2),nrow)

like in

ddply(data,.(col1,col2),function(x) data.frame(number=nrow(x)))

回答1:


Perhaps you are looking for summarize (or mutate or transform, depending on what you want to do).

A small example:

set.seed(1)
data <- data.frame(col1 = c(1, 2, 2, 3, 3, 4),
                   col2 = c(1, 2, 2, 1, 2, 1),
                   z = rnorm(6))
ddply(data,.(col1,col2), summarize, 
      number = length(z), newcol = mean(z))
#   col1 col2 number     newcol
# 1    1    1      1 -0.6264538
# 2    2    2      2 -0.3259926
# 3    3    1      1  1.5952808
# 4    3    2      1  0.3295078
# 5    4    1      1 -0.8204684


来源:https://stackoverflow.com/questions/18442555/set-column-name-ddply

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!