Monday, October 9, 2017

sd() {base}


sd() function that computes the standard deviation of the values in x.

sd(x, na.rm = FALSE)

The arguments are:
  • x: numeric vector
  • na.rm: logical value indicating whether NA values should be removed before the computation proceeds.

Formula to calculate the Standard Deviation:
σ=(xix¯)2n1
SD quantifies how much the members of a group differ from the mean value for the group.

x = c(1,6,10,23,4,5,56)
sd1 = sqrt(sum((x - mean(x))^2)/(length(x)-1))
sd2 = sd(x)
sd1 ; sd2 #same result
## [1] 19.44222
## [1] 19.44222

na.rm:
y = c(1,6,10,23,NA, NA, 4,5,56, 56, NA)
sd3 = sqrt(sum((y - mean(y))^2)/(length(y)-1))
sd4 = sd(y)
sd5 = sd(y, na.rm = TRUE) #remove NA values to compute the SD
sd3 ; sd4 ; sd5
## [1] NA
## [1] NA
## [1] 23.11114
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
meanset = mean(iris$Sepal.Length[iris$Species=='setosa'])
meanversi = mean(iris$Sepal.Length[iris$Species=='versicolor'])
meanvir = mean(iris$Sepal.Length[iris$Species=='virginica'])

sdset = sd(iris$Sepal.Length[iris$Species=='setosa'])
sdversi = sd(iris$Sepal.Length[iris$Species=='versicolor'])
sdvir = sd(iris$Sepal.Length[iris$Species=='virginica'])
plot(iris$Species, iris$Sepal.Length, col = 'lightblue', main = 'SD as a measure of spread')
segments(1, meanset+sdset,1, meanset-sdset, col = 'deeppink', lwd = 5)
segments(2, meanversi+ sdversi, 2, meanversi-sdversi, col = 'deeppink', lwd = 5)
segments(3, meanvir + sdvir/2, 3, meanvir-sdvir/2, col = 'deeppink',lwd = 5)

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') 

Wednesday, October 4, 2017

min() pmin() max() pmax() {base}


min() and max() return the minima and maxima of the input values.
pmin() and pmax() return the parallel minima and maxima of the input values.

max(..., na.rm = FALSE) min(..., na.rm = FALSE)
pmax(..., na.rm = FALSE) pmin(..., na.rm = FALSE)

The arguments are:
 - ...: numeric or character arguments
 - na.rm: logical indicating whether missing values should be removed

max() and min():
x = 1:11
y = c(6:11 , 7:3)
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11
y
##  [1]  6  7  8  9 10 11  7  6  5  4  3
max(x); min(x)
## [1] 11
## [1] 1
max(y); min(y)
## [1] 11
## [1] 3
summary(airquality[1:4])
##      Ozone           Solar.R           Wind             Temp      
##  Min.   :  1.00   Min.   :  7.0   Min.   : 1.700   Min.   :56.00  
##  1st Qu.: 18.00   1st Qu.:115.8   1st Qu.: 7.400   1st Qu.:72.00  
##  Median : 31.50   Median :205.0   Median : 9.700   Median :79.00  
##  Mean   : 42.13   Mean   :185.9   Mean   : 9.958   Mean   :77.88  
##  3rd Qu.: 63.25   3rd Qu.:258.8   3rd Qu.:11.500   3rd Qu.:85.00  
##  Max.   :168.00   Max.   :334.0   Max.   :20.700   Max.   :97.00  
##  NA's   :37       NA's   :7
min(airquality[1:4], na.rm = FALSE) #there are missing values, and the result is NA
## [1] NA
min(airquality[1:4], na.rm = TRUE)  # missing values are removed
## [1] 1
max(airquality[1:4], na.rm = FALSE) #there are missing values, and the result is NA
## [1] NA
max(airquality[1:4], na.rm = TRUE)  # missing values are removed
## [1] 334

pmax() and pmin():
x
##  [1]  1  2  3  4  5  6  7  8  9 10 11
y
##  [1]  6  7  8  9 10 11  7  6  5  4  3
pmax(x,y) 
##  [1]  6  7  8  9 10 11  7  8  9 10 11
pmin(x,y)
##  [1] 1 2 3 4 5 6 7 6 5 4 3

x and y have the same length, but if the vectors to be compared don’t have the same length numbers are recycled:
z = 1:14
pmax(z,y) 
## Warning in pmax(z, y): an argument will be fractionally recycled
##  [1]  6  7  8  9 10 11  7  8  9 10 11 12 13 14
pmin(z,y)
## Warning in pmin(z, y): an argument will be fractionally recycled
##  [1] 1 2 3 4 5 6 7 6 5 4 3 6 7 8

When we work with NA:
i = c(NA, 10:15, NA)
j = c(3:6, 16:19)
i;j
## [1] NA 10 11 12 13 14 15 NA
## [1]  3  4  5  6 16 17 18 19
pmin(i,j) #the function returns NA values
## [1] NA  4  5  6 13 14 15 NA
pmax(i,j) #the function returns NA values
## [1] NA 10 11 12 16 17 18 NA
pmin(i,j, na.rm = TRUE)  #NA values are removed
## [1]  3  4  5  6 13 14 15 19
pmax(i,j, na.rm = TRUE)  #NA values are removed
## [1]  3 10 11 12 16 17 18 19
plot(i, ylim = c(1,20), type = 'b', ylab = 'Values')
points(j, col = 'grey', type = 'b')
points(pmin(i,j), col = 'darkviolet', type = 'l')
points(pmax(i,j), col = 'deeppink', type = 'l')
legend("topleft", inset=.05, c("i","j","pmin(i,j)", "pmax(i,j)"), fill=c('black','grey','darkviolet', 'deeppink'))

duplicated() {base}

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