Set ddply column name

How to set column name of summarized data in

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

how in

 ddply(data,.(col1,col2),function(x) data.frame(number=nrow(x))) 
+4
source share
1 answer

You might be looking for summarize (either 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 
+6
source

Source: https://habr.com/ru/post/1498941/


All Articles