Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace all row values according to a value in a specific column in R

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading