Emilio M. Bruna - 4 months ago 20

R Question

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

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
```