1

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.

2 Answers2

2

You need to arrange the columns in a way such that the test column (age) is the last.

library(dplyr)
df %>%
  select(x, y, age) %>%
  mutate_all(~replace(.x, age> 90, NA))

#   x  y age
#1  1  1   1
#2  2  2  10
#3 NA NA  NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I like this one but it does not seem intuitive to me that leaving out `select(x, y, age) %>%` gives other results. Do you maybe know why this is the case? –  Jan 27 '20 at 10:13
  • @machine yes, because in your original `df` we have first column as `age`, when we use `mutate_all` it replaces values of `age` column which has `age > 90` to `NA`. For 2nd column those `age` values are already `NA`, hence no condition satisfies for `age> 90`. for rest of the columns. – Ronak Shah Jan 27 '20 at 10:17
0
library(dplyr)

df <- data.frame(age=c(1,10, 100), x= 1:3, y= 1:3)
df[df$age > 90, ] <- NA


df %>%
    mutate_all(function(i) replace(i, .$age> 90, NA))

  age  x  y
1   1  1  1
2  10  2  2
3  NA NA NA

Just to be sure. You're saying, you want to exclude them. I would guess that you actually maybe want this:

df %>%
    filter(age <= 90)

  age x y
1   1 1 1
2  10 2 2

?

Georgery
  • 7,643
  • 1
  • 19
  • 52
  • I know, usually we would use `filter` but for some reasons I want to keep the dimensions of the data as it is. Thank you for that awesome answer! –  Jan 27 '20 at 10:05
  • OK, got it. I would leave the answer as it is though for other readers, ok? – Georgery Jan 27 '20 at 10:06