Search code examples
rif-statementdplyracross

Creating a new variable and altering dependent variables in r using ifelse


Let's say we have a df as follows:

A  B  C  D  E
1  1  0  0  1
0  0  1  0  0
0  0  0  0  1
1  1  1  1  0
0  1  1  0  1
1  0  1  0  0   

So I would like to make another variable F which says, if the sum of A:D is greater than 1, F is 1 and A:D are 0.

Additionally, If E == 1, then F = 0.

So here's how I wrote it but it's not working...

#Counter
df<- df %>% 
       mutate(case_count = A+B+C+D)

df$F <- ifelse(df$E == 1, 0,
              ifelse(df$case_count > 1, 
                     df$A == 0 & 
                     df$B == 0 &
                     df$C == 0 &
                     df$D == 0 &
                     df$F == 1, 0))

And the correct result here should then be

A  B  C  D  E  case_count F 
1  1  0  0  1           2 0
0  0  1  0  0           1 0
0  0  0  0  1           0 0
0  0  0  0  0           4 1
0  1  1  0  1           2 0
0  0  0  0  0           2 1

Solution

  • Using dplyr and the new functions across and c_across

    df %>% 
      rowwise() %>% 
      mutate(
        case_count = sum(c_across(A:D)),
        F_ = ifelse(E == 1, 0, ifelse(case_count > 1, 1, 0))
        ) %>% 
      mutate(across(A:D, ~ifelse(F_ == 1, 0, .)))
    

    I named the new column F_ instead of just F because the latter may be confused with the abbreviation for FALSE.

    Output

    # A tibble: 6 x 7
    # Rowwise: 
    #       A     B     C     D     E case_count    F_
    #   <dbl> <dbl> <dbl> <dbl> <int>      <int> <dbl>
    # 1     1     1     0     0     1          2     0
    # 2     0     0     1     0     0          1     0
    # 3     0     0     0     0     1          0     0
    # 4     0     0     0     0     0          4     1
    # 5     0     1     1     0     1          2     0
    # 6     0     0     0     0     0          2     1