Search code examples
rdata-cleaningdata-wrangling

Need to modify values across rows according to a condition


I am cleaning my data with R and I want to change the values across a group of variables according to one condition that applies to all the values of each case for this group of variables

I have 4 that have three possible answers: "Yes", "No" and NA. I need to recode the values of some cases that right now are introduced as NA and I want to them to be introduced as No

Those cases that, across the 4 variables have NA's and 1, but no "0", I want to convert the NA's to "0". While in those cases where 1, 0 and Na coexist, I want to leave the NA's like that.

For instance, I have created this data.frame

A = c(1,0,1,NA,NA,NA,NA,1,0,1,1)
B = c(1,0,1,1,1,NA,1,1,0,0,1)
C = c(0,0,NA,1,0,0,1,1,1,0,1)
D = c(0,0,1,0,1,0,0,1,0,1,1)

data <- data.frame(A,B,C,D)

Which looks like this: In this case, I would like to make a formula that changes the NA values of the third case for 0. Not simply recording the third case but as a function that I can apply to a database with thousands of cases... How could I do this?

    A  B  C D
1   1  1  0 0
2   0  0  0 0
3   1  1 NA 1
4  NA  1  1 0
5  NA  1  0 1
6  NA NA  0 0
7  NA  1  1 0
8   1  1  1 1
9   0  0  1 0
10  1  0  0 1
11  1  1  1 1

I have tried to do recoding, for loop and filters but I haven't got it right.

Perhaps a rowwise with a mutate could work?

Many thanks in advance!!


Solution

  • Sorry, I somehow missed the explanation:

    library(dplyr)
    
    data |> 
      rowwise() |> 
      mutate(check_0 = sum(if_else(c_across(A:D) == 0, 1, 0, missing = 0))) |> 
      ungroup() |> 
      mutate(across(A:D, ~ if_else(check_0 == 0 & is.na(.x), 0, .x))) |> 
      select(-check_0)
    
    # A tibble: 11 × 4
           A     B     C     D
       <dbl> <dbl> <dbl> <dbl>
     1     1     1     0     0
     2     0     0     0     0
     3     1     1     0     1
     4    NA     1     1     0
     5    NA     1     0     1
     6    NA    NA     0     0
     7    NA     1     1     0
     8     1     1     1     1
     9     0     0     1     0
    10     1     0     0     1
    11     1     1     1     1