Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fastest summarise on a conditional subset

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading