I struggle with dplyr because I want to do two things on one and think about whether this is possible.
I want to calculate the average value and at the same time the average value for values โโthat have a specific value in another column.
library(dplyr) set.seed(1234) df <- data.frame(id=rep(1:10, each=14), tp=letters[1:14], value_type=sample(LETTERS[1:3], 140, replace=TRUE), values=runif(140)) df %>% group_by(id, tp) %>% summarise( all_mean=mean(values), A_mean=mean(values),
Therefore, the column A_mean must calculate the average value, where value_count == 'A' .
Usually I make two separate commands and combine the results later, but I think there is a more convenient way, and I just donโt understand.
Thanks in advance.
source share