#If data (e.g. X) contain missing values (typically, NAs)
mean(X) #will return NA
mean(X,na.rm=TRUE) #will omit NAs and then do calculation correctly
#It is important to remember that if you are developing a function like tapply or lapply that
#itself calls functions like mean(), those will also return NAs if you don't make sure that
#that tapply/lapply calls the function with na.rm included.
#For example, see meanByGroupDS. The line:
FUN <- function (x) {UseMethod("mean")}
#returns NAs when lapply calls FUN
#So you must use:
FUN <- function(x) {mean(x,na.rm=TRUE)}
|