Skip to content
Prev 816 / 885 Next

Need help to understand the code

Hi Chenguang Du,

This is really a better question for R-help as R-Sig-Teaching is about
teaching statistics with R. But ?

This function:

mystats <- function(x, na.omit=FALSE){
 if (na.omit)
 x <- x[!is.na(x)]
 m <- mean(x)
 n <- length(x)
 s <- sd(x)
 skew <- sum((x-m)^3/s^3)/n
 kurt <- sum((x-m)^4/s^4)/n - 3
 return(c(n=n, mean=m, stdev=s, skew=skew, kurtosis=kurt))
 }

is equivalent to:

mystats <- function(x, na.omit=FALSE){if (na.omit){
   x <- x[!is.na(x)]
}
m <- mean(x)
 n <- length(x)
 s <- sd(x)
 skew <- sum((x-m)^3/s^3)/n
 kurt <- sum((x-m)^4/s^4)/n - 3
 return(c(n=n, mean=m, stdev=s, skew=skew, kurtosis=kurt))
 }

So, the authors of that function wanted that if() statement to apply only
to the line immediately below it (i.e., this code x <- x[!is.na(x)]) and
not the rest of the function.
On Fri, Jun 14, 2019 at 1:19 PM Chenguang Du <dcheng6 at vt.edu> wrote: