I would like to pass the length of my group_by variable to summarize.
Example data
set.seed(112)
df <- data.frame(
groupper = factor(sample.int(n = 12, size = 100, replace = TRUE)),
var = runif(100, min = 1, max = 25)
)
Now I have a different number of factors:
table(df[,1])
1 2 3 4 5 6 7 8 9 10 11 12
8 7 4 8 9 7 10 7 11 3 13 13
Now I would like to simply find the share of var
in each groupper
in certain intervals.
My code looks like this:
results <- df %>% group_by(groupper) %>% summarise(
var0_25 = sum(var < 25 / length(groupper)),
var25_50 = sum(var >= 25 & var < 50) / length(groupper))
#etc...
)
But, how in the world do I get the correct group_by(groupper)
length into my summarize
? It changes for each factor.