justin
justin

Reputation: 23

R, dplyr, Conditionally change values in multiple columns based on one conditional column in R

Given the following data frame:

df <- data.frame("a" = 1:5, "b" = 2:6, "c" = 3:7, "d" = c(NA,1,1,0,0))

How can I change values in columns a, b, and c to NA if values in column d are either NA or 0? I can get it to work easily for individual columns, e.g., df[,3][df$d==0|is.na(df$d)] <- NA, but I'm having trouble getting something to work across multiple columns. I'd very much appreciate solutions in base R or dplyr. Thanks

Upvotes: 2

Views: 2531

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

Do you mean this?

cols <- c("a", "b", "c")
df[is.na(df$d) | df$d == 0, cols] <- NA
df
#   a  b  c  d
#1 NA NA NA NA
#2  2  3  4  1
#3  3  4  5  1
#4 NA NA NA  0
#5 NA NA NA  0

Or in dplyr

library(dplyr)
df %>% mutate_at(vars(a:c), funs(ifelse(is.na(d) | d == 0, NA, .)))
#   a  b  c  d
#1 NA NA NA NA
#2  2  3  4  1
#3  3  4  5  1
#4 NA NA NA  0
#5 NA NA NA  0

Upvotes: 7

Related Questions