I want to exclude participants from an analysis that are too old (age >90). Usually I would do it like that:
df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3)
df[df$age > 90, ] <- NA
I can't figure out how to do this with dplyr. If we want to replace one variable we can use
library(dplyr)
df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3)
df %>%
mutate(age= replace(age, age> 90, NA))
So I thought I could use
df %>%
mutate_all(function(i) replace(i, age> 90, NA))
I also tried mutate_if
and mutate_at
but it did not work out. After reading questions on SO I think the "problem" is that in my situation I need to change the values rowwise with dplyr.