Is there a way to exclude NA while calculating outliers in a data frame but still include rows with NA in the final output?

Viewed 100

For instance, I am removing outliers greater than 3SD but there are NAs in my dataset. I would like to calculate the SD, remove those outliers ignoring those with NAs in the calculation, but retaining those with NAs in the final output. It seems using na.rm function would remove NAs in the final data frame produced, so I'm wondering if there's another way to go about this?

Current code I have is:

df_final = df %>% 
  filter(height < mean(height, na.rm = T) + (3*sd(df$height, na.rm=T)))

Thank you!

1 Answers

We can have an | condition with is.na to not remove NA rows

library(dplyr)
df %>% 
   filter((height < (mean(height, na.rm = TRUE) +
                    3* sd(height, na.rm=TRUE)))|is.na(height))
Related