I want to ignore NA in a vector that I want to calculate the mscore. I do not want to use na.omit as this will change the length of my vector that I need for other purposes later on.
What I have so far is:
library("outliers")
myvector <- c(0.00750,0.04750,0.06500,0.04750,0.0150,0.00750,0.210,0.02525,0.05750,NA)
mscore <- scores(myvector, "mad")
This produces all NA and there does not appear to be support for NAs in the scores function.
Without using na.omit
, is there a work around?
Write a new function that internally handles the NA entries:
scores_na <- function(x, ...) {
not_na <- !is.na(x)
scores <- rep(NA, length(x))
scores[not_na] <- outliers::scores(na.omit(x), ...)
scores
}
myvector <- c(0.00750,0.04750,0.06500,0.04750,0.0150,0.00750,0.210,0.02525,0.05750,NA)
scores_na(myvector, "mad")
[1] -1.2125677 0.0000000 0.5304984 0.0000000 -0.9852112 -1.2125677
[7] 4.9260561 -0.6744908 0.3031419 NA