Home > OS >  How to keep N/A as N/A when using an ifelse statement?
How to keep N/A as N/A when using an ifelse statement?

Time:11-12

I am recoding a df with columns containing "yes," "no," or "N/A." I want to recode "yes" as 1, "no" as 0, and keep "N/A" as "N/A."

I have written the following code:

df$first_column <-ifelse(df$first_column=="yes",1,0)

However, this replaces N/A with 0. What is the easiest way to get around this problem?

Here is reproducible df:

structure(list(first_column = c("yes", "no", "N/A", "yes")), row.names = c(NA, 
4L), class = "data.frame")

CodePudding user response:

Try with either a nested ifelse if we want to avoid any packages

df$first_column <- ifelse(df$first_column == "N/A", "N/A", 
     ifelse(df$first_column == "yes",1,0))

-output

df$first_column
[1] "1"   "0"   "N/A" "1"  

NOTE: "N/A" is not NA. It is better to convert to NA as the "N/A" is character and this will change the integer expected binary column to character. Better will be

with(df, replace(first_column, first_column == "N/A", NA) == "yes")
[1]  TRUE FALSE    NA  TRUE

Or could use recode

library(dplyr)
 df %>%
   mutate(first_column = recode(first_column, yes = 1, no = 0))

-output

  first_column
1            1
2            0
3           NA
4            1

CodePudding user response:

If you want to avoid nested if_else

df <-mutate(df, first_column = case_when(first_column=="yes" ~ 1,
                                         first_column=="no" ~ 0,
                                         T ~ first_column))
  • Related