Home > Back-end >  How to mutate two var with values 0, 1 & NA into a var with the sum of 0 and 1?
How to mutate two var with values 0, 1 & NA into a var with the sum of 0 and 1?

Time:01-23

I'm having a problem where I want to mutate two variables with values 0, 1 and NA into a new variable with the sum of 0 and 1, however, R in my case counts NA as 0 or return only NA. Are there an easy fix to this, to exclude the NA? These variables are a part of a large dataset. And in this dataset I have some survey experiments, these two var being part of it, which means I have NA in every single row. So a simple drop of NA is a not a practical approach.

The two var I want to sum are:

table(df$naked_fj, useNA = "ifany")
#>    0    1 <NA> 
#>  127   81  570 

table(df$naked_naked, useNA = "ifany")
#>    0    1 <NA> 
#>  117   82  579

The result should be:

#>    0    1 <NA> 
#>  244   163  x(or excluded)

I am open to converting to char. etc. whatever works.

Data example:

naked_fj naked_naked
NA 1
0 NA
1 NA
0 NA
NA 0
NA NA
NA NA

Codes I have tried:

library(tidyverse)

df <- df |> 
  mutate((naked_man = naked_fj   naked_naked), na.rm = TRUE)

Returns all OBS as NA

I thought this would fix it

library(tidyverse)
df <- df |> rowwise() |> mutate(naked_man = sum(c(naked_fj, naked_naked), na.rm = TRUE))

And it gave me: 0 = 615, 1 = 163. Ergo NA is being counted as zero.

CodePudding user response:

Alternatively, you may check the below code where I used 'rowSums' with across

library(tidyverse)

df2 <- df %>%  mutate(naked_man = rowSums(across(c(naked_fj, naked_naked)), na.rm = TRUE))

Created on 2023-01-21 with reprex v2.0.2

# A tibble: 5 × 3
  naked_fj naked_naked naked_man
     <dbl>       <dbl>     <dbl>
1       NA           1         1
2        0          NA         0
3        1          NA         1
4        0          NA         0
5       NA           0         0

CodePudding user response:

The following code solved it:

df <- df |>  
  mutate(naked_man = coalesce(naked_fj, naked_naked)) |>
  mutate(naked_man = na_if(naked_man, 88)) |>
  mutate(naked_man = na_if(naked_man, 99))
  • Related