Home > Blockchain >  Conditionally replace values with NA in R
Conditionally replace values with NA in R

Time:04-20

I'm trying to conditionally replace values with NA in R.

Here's what I've tried so far using dplyr package.

Data

have <- data.frame(id = 1:3,
                   gender = c("Female", "I Do Not Wish to Disclose", "Male"))

First try

want = as.data.frame(have %>% 
        mutate(gender = replace(gender, gender == "I Do Not Wish to Disclose", NA))
    )

This gives me an error.

Second try

want = as.data.frame(have %>% 
        mutate(gender = ifelse(gender == "I Do Not Wish to Disclose", NA, gender))
    )

This runs without an error but turns Female into 1, Male into 3 and I Do Not Wish to Disclose into 2...

CodePudding user response:

It is case where the column is factor. Convert to character and it should work

library(dplyr)
have %>% 
    mutate(gender = as.character(gender), 
    gender = replace(gender, gender == "I Do Not Wish to Disclose", NA))

The change in values in gender is when it gets coerced to its integer storage values

as.integer(factor(c("Male", "Female", "Male")))

CodePudding user response:

I would use the very neat function na_if() from dplyr.

library(dplyr)

have <- data.frame(gender = c("F", "M", "NB", "I Do Not Wish to Disclose"))

have |> mutate(gender2 = na_if(gender, "I Do Not Wish to Disclose"))

Output:

#>                      gender gender2
#> 1                         F       F
#> 2                         M       M
#> 3                        NB      NB
#> 4 I Do Not Wish to Disclose    <NA>

Created on 2022-04-19 by the reprex package (v2.0.1)

  • Related