r - Can I create multiple columns from a single group by mutate? -
i group dataframe on column , apply function grouped data returns multiple columns. means of example, consider following
names = append(rep('mark',10),rep('joe',10)) spend = rnorm(length(names),50,0.5) df <- data.frame( names, spend ) get.mm <- function(data){ return(list(median(data),mean(data))) }
here, get.mm
returns list of 2 numbers. apply get.mm
df %>% group_by(names)
, have result have 2 columns, 1 fo each output of function.
the desired result should be
names median mean <fctr> <dbl> <dbl> 1 joe 49.89284 49.9504 2 mark 50.17244 50.0735
i've simplified function here means of demonstration, know
df %>% group_by(names) %>% summarise(median = median(spend), mean = mean(spend))
if rewrite get.mm
returns data frame, can use group_by %>% do
:
get.mm <- function(data){ data.frame(median = median(data), mean = mean(data)) } df %>% group_by(names) %>% do(get.mm(.$spend)) # here . stands sub data frame unique name, .$spend passes corresponding # column function
a reproducible example:
set.seed(1) names = append(rep('mark',10),rep('joe',10)) spend = rnorm(length(names),50,0.5) df <- data.frame(names, spend) df %>% group_by(names) %>% do(get.mm(.$spend)) # tibble: 2 x 3 # groups: names [2] # names median mean # <fctr> <dbl> <dbl> #1 joe 50.24594 50.12442 #2 mark 50.12829 50.06610 df %>% group_by(names) %>% summarise(median = median(spend), mean = mean(spend)) # tibble: 2 x 3 # names median mean # <fctr> <dbl> <dbl> #1 joe 50.24594 50.12442 #2 mark 50.12829 50.06610
Comments
Post a Comment