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

Recoding integers as NA

I have a continuous variable within a large dataset (df) that contains both NA’s and entries that should be recoded as NA’s. The variable in question (optime) is an integer. I would like to keep the existing NA’s and recode non-sensical values as NA’s. optime should have values that are typically >120.

df$optime %>% summary()

Gives me:

Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
4.0   280.0   358.0   371.9   449.0  1324.0       6 

Clearly, the lower values are erroneous. So I would like to replace them:

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

df <- df %>%
mutate(optime = na_if(optime, <5, NA))

This code has worked to recode cells with 0 values, but I gather that it does not accept conditional operators such as ‘<‘.

I have also tried with if_else:

mutate(optime = if_else(optime <5, NA, optime))

I am certain that this is pretty simple for most users, but I am not finding an answer. Should I use case_when? Thank you.

>Solution :

optime = ifelse(optime <5, NA, optime)
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