a = tibble(x = runif(1000,0,10),
t = rpois(1000,4),
s = sample(c(0,1),1000, replace = T)
) %>% group_by(t)
I want to summarise twice mean(x) in a.
First variable b1 is mean(x) when s == 0
Second variable b2 is mean(x) when s == 1
Which is the fastest way within a summarise?
Important: I don’t want to group_by(s), which is probably the fastest option… the output would be confusing.
>Solution :
Unclear to me if you specifically want the summaries with t still grouped. This base R approach ignores the grouping.
a$b1 <- mean(a$x[a$s == 0])
a$b2 <- mean(a$x[a$s == 1])
Or a dplyr approach that keeps the grouping of t:
a %>%
summarize(b1 = mean(x[s==0]),
b2 = mean(x[s==1]))