Emilio M. Bruna Emilio M. Bruna - 1 year ago 61
R Question

calculate means for combinations of 2 columns with dplyr

I'm hoping for help generating the means for combinations multiple columns of a df using dplyr, and -assuming one can - I can't get the syntax right.

df <- data.frame(trt1=c(replicate(6, "far"), replicate(6, "near")),
trt2=rep(c("A","A","A","B","B","B"), 2),
count=sample(1:8, 12, replace=T))

I know how to get the means by one column:

df %>% group_by(trt1) %>% summarise(avg=mean(count))

But can't figure out the syntax for combinations of columns: far x A, far x B, near x A, near x B.

Thanks much.

Answer Source

Add trt2 to the group_by statement.

df %>% group_by(trt1,trt2) %>% summarise(avg=mean(count))

    trt1   trt2      avg
  <fctr> <fctr>    <dbl>
1    far      A 3.000000
2    far      B 1.333333
3   near      A 5.000000
4   near      B 4.666667
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download