5

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)))
Klaus
  • 1,946
  • 3
  • 19
  • 34
  • 1
    Did you look at the very first example of `?ddply`? – Henrik Aug 26 '13 at 11:13
  • Also, what's wrong with your *like in* example? – flodel Aug 26 '13 at 11:18
  • 1
    @Henrik, if you mean `ddply(dfx, .(group, sex), summarize, mean = round(mean(age), 2), sd = round(sd(age), 2))` then I do, but it looks so different from the explanation of the function declaration that I cant map the third and fourth parameter. – Klaus Aug 26 '13 at 12:56
  • I agree with Klaus here that the documentation structure could be improved. If you don't know about `summarize` and so on, it can be a bit confusing what's going on and what needs to be done. – A5C1D2H2I1M1N2O1R2T1 Aug 26 '13 at 17:35

1 Answers1

7

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
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485