Thanks for the solution Christophe - very nice!
It took me a while to work out that there is some "short-hand" in your solution, since this is not obvious (to me) in the ?case_when help.
The "full" code would be (using the latest version of dplyr):
df %>%
group_by(bb) %>%
mutate_at(vars(dd, ee, ff),
list(~ case_when(
any(cc == "s2") ~ .,
FALSE ~ NA_real_)))
The reverse logic also works:
df %>%
group_by(bb) %>%
mutate_at(vars(c(3:5)),
~ case_when(
!any(cc == "s2") ~ NA_real_,
TRUE ~ .))
But this non-default approach will fail if the result columns are of mixed type. Its great that the defaults detect this and insert the appropriate NAs:
set.seed(1984)
df_2 <- tibble(bb=factor(rep(c(1,2,3,4), each=5)),
cc=factor(c(rep(c("s1","s2","s3","s3","s3","s1","s3","s3","s4","s5"), times=2))),
dd=c(seq(11,30,1)),
ee=rnorm(n=20, mean=20, sd=4),
ff=factor(rep(c("pqr", "xyz"), times=10))) # Changed column type
df_2
str(df_2)
df_2 %>%
group_by(bb) %>%
mutate_at(vars(dd, ee, ff),
~ case_when(any(cc == "s2") ~ .))
Thanks again.