Let's say that we have a dataset that look like this:
var | c1 | c2 | c3 |
---|---|---|---|
a | TRUE | TRUE | TRUE |
b | FALSE | TRUE | TRUE |
c | TRUE | TRUE | TRUE |
d | FALSE | TRUE | TRUE |
I want to replace the all the row values (right wise not the var column) according to the FALSE value of column c1 with NA.
Ideally I want to look like this :
var | c1 | c2 | c3 |
---|---|---|---|
a | TRUE | TRUE | TRUE |
b | FALSE | NA | NA |
c | TRUE | TRUE | TRUE |
d | FALSE | NA | NA |
var = c("a","b","c","d")
c1 = c(TRUE,FALSE,TRUE,FALSE)
c2 = c(TRUE,TRUE,TRUE,TRUE)
c3 = c(TRUE,TRUE,TRUE,TRUE)
data= tibble(var,c1,c2,c3);data
How can I do it in R using the dplyr package ? Any help ?
CodePudding user response:
We can use across
in dplyr
- loop across
the 'c2', 'c3' columns, and use the logical column from 'c1' to return the values of the column, by default the last condition i.e. TRUE
will be all NA
library(dplyr)
data <- data %>%
mutate(across(c2:c3, ~ case_when(c1 ~ .x)))
-output
data
# A tibble: 4 × 4
var c1 c2 c3
<chr> <lgl> <lgl> <lgl>
1 a TRUE TRUE TRUE
2 b FALSE NA NA
3 c TRUE TRUE TRUE
4 d FALSE NA NA