I am practicing writing functions and testing them. I have written the following functions but when I try to specify na.rm=TRUE my NA's are not being removed.
my.mean <- function(x, na.rm) {
stopifnot(is.numeric(x))
answer <- sum(x)/length(x)
return(answer)
}
My test vector is
t <- c(12,14,NA,1)
Result
my.mean(t, na.rm = TRUE)
[1] NA
I am not sure why I am not getting the answer 9. I have tried specifying
na.rm = FALSE
in the original function but know I should not have to.
Any ideas?
Thank you.
PS I know mean is a built in function for R I am just practicing. Also it is happening for my other written functions.