Fastest summarise on a conditional subset

Solution 1:

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]))