Home > Software design >  Recoding integers as NA
Recoding integers as NA

Time:09-09

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:

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.

CodePudding user response:

optime = ifelse(optime <5, NA, optime)
  • Related