I have the following function to describe a variable
library(dplyr) describe = function(.data, variable){ args <- as.list(match.call()) evalue = eval(args$variable, .data) summarise(.data, 'n'= length(evalue), 'mean' = mean(evalue), 'sd' = sd(evalue)) }
I want to use dplyr to describe a variable.
set.seed(1) df = data.frame( 'g' = sample(1:3, 100, replace=T), 'x1' = rnorm(100), 'x2' = rnorm(100) ) df %>% describe(x1)
The problem is that when I try to apply the same descriptive function group_by , the description function is not applied in every group
df %>% group_by(g) %>% describe(x1) # # A tibble: 3 x 4 # gn mean sd # <int> <int> <dbl> <dbl> # 1 1 100 -0.01757949 0.9400179 # 2 2 100 -0.01757949 0.9400179 # 3 3 100 -0.01757949 0.9400179
How would you change the function to get what you want using a small number of modifications?
r dplyr
marc1s
source share