Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is reproducible df:

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading