Friday, October 6, 2017

mean() {base}


mean() function that computes the arithmetic mean.

mean(x, trim = 0, na.rm = FALSE, ...)

The arguments are:
  • x: numeric vector
  • trim: fraction of observations (0 to 0.5) to be trimmed from each end of x before the mean is computed.
  • na.rm: logical value indicating whether NA values should be removed before the computation proceeds.

x = 1:10; x
##  [1]  1  2  3  4  5  6  7  8  9 10
mean(x)  #sum(x)/length(x)
## [1] 5.5

trim
It trims a fraction (0 to 0.5) of observations from each end of x before the mean is computed.
y = c(7,8,9,3,4,55,3,4,5,6,2,23,4,5,3,65,5,78,9,9)
mean(y)
## [1] 15.35
sort(y)
##  [1]  2  3  3  3  4  4  4  5  5  5  6  7  8  9  9  9 23 55 65 78
mean(y, trim = 0.1) ; e = sort(y)[-c(1,2,19,20)] ; sum(e)/length(e)
## [1] 9.9375
## [1] 9.9375
mean(y, trim = 0.3) ; f = sort(y)[-c(1:6,15:20)] ; sum(f)/length(f)
## [1] 6.125
## [1] 6.125
# if trim >= to 0.5 it returns the median
mean(y, trim = 0.5) ; d = sort(y)[-c(1:10,10:20)] ; sum(d)/length(d) 
## [1] 5.5
## [1] NaN
mean(y, trim = 0.9)
## [1] 5.5

na.rm:
 na.rm = TRUE removes NA values before computing the mean.
z = c(1,2,3,4,5,NA,NA,23,34,67)
mean(z)
## [1] NA
mean(z, na.rm = TRUE)
## [1] 17.375
plot(sort(y), main = 'mean() function')
abline(v = mean(y), col = 'deeppink')
abline(v = mean(y, trim = 0.1), col = 'orange')
abline(v = mean(y, trim = 0.6), col = 'darkmagenta') #median
text(16.5, 60, "mean(y)", col = 'deeppink') 
text(11.2, 60, "mean(y,\n trim = 0.1)", col = 'orange') 
text(6.9, 60, "mean(y,\n trim = 0.6) \n (median)", col = 'darkmagenta') 

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...