Sunday, November 5, 2017

duplicated() {base}


duplicated() function determines which elements are duplicated and returns a logical vector.

The parameters of the function are:
 x: vector, array or data frame
 fromLast:logical indicating if duplication should be considered from the last
x = c(5:1,5:1,5)
x
##  [1] 5 4 3 2 1 5 4 3 2 1 5
duplicated(x); duplicated(x, fromLast = TRUE)
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
## extract duplicated elements, those elements that duplicated(x) == TRUE, may be repeted elements:
x[duplicated(x)]
## [1] 5 4 3 2 1 5
## extract unique elements
x[!duplicated(x)]
## [1] 5 4 3 2 1
## extract unique elements starting from the righmost value (different order):
x[!duplicated(x, fromLast = TRUE)]
## [1] 4 3 2 1 5
#duplicated using a data frame
duplicated(iris)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [111] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [122] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [144] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
duplicated(iris$Sepal.Length)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
##  [12] FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [23]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [34] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [45]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [56]  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [67]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [78]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [89]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [100]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE
## [111]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [122]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
## [133]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [144]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
anyDuplicated() function returns the position of the first element duplicated:
x = c(9:1, 20, 10:6,21,10)
x
##  [1]  9  8  7  6  5  4  3  2  1 20 10  9  8  7  6 21 10
duplicated(x)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
anyDuplicated(x)  #first element found to be duplicated
## [1] 12
anyDuplicated(x, fromLast = TRUE) #first element found to be duplicated
## [1] 11
duplicated(iris)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [100] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [111] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [122] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
## [144] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
anyDuplicated(iris) ## 143
## [1] 143
x <-c(1:10, 1:10)
x
##  [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10
par(mfrow = c(1,2))
plot(x, col = 'chocolate1', pch = 21, bg = 'chocolate1')
plot(x[duplicated(x)], col = 'chocolate4', pch = 21, bg = 'chocolate4')
y <-c(1:10, 1:10, 1:10)
y
##  [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10  1  2  3
## [24]  4  5  6  7  8  9 10
par(mfrow = c(1,2))
plot(y, col = 'pink', pch = 21, bg = 'pink')
plot(y[duplicated(y)], col = 'red', pch = 21, bg = 'red' )

Tuesday, October 24, 2017

unique() {base}


unique() function is a generic function that extracts unique values from a vector, array or data frame.

The parameters of the function are:
-x: vector, array or data frame to remove duplicated values
-fromLast: logical indicating if duplication should be considered from the last

#`unique()` function using vectors:
x = c(10 + 0:5, 1:5, 8:1)
x
##  [1] 10 11 12 13 14 15  1  2  3  4  5  8  7  6  5  4  3  2  1
u1 = unique(x)
u1
##  [1] 10 11 12 13 14 15  1  2  3  4  5  8  7  6
u2 = unique(x,  fromLast = TRUE) # different order
u2
##  [1] 10 11 12 13 14 15  8  7  6  5  4  3  2  1
y = c(5:1,8:1, 10, 1:3)
y
##  [1]  5  4  3  2  1  8  7  6  5  4  3  2  1 10  1  2  3
u3 = unique(y)
u3
## [1]  5  4  3  2  1  8  7  6 10
u4 = unique(y,  fromLast = TRUE) # different order
u4
## [1]  8  7  6  5  4 10  1  2  3
#`unique()` function with data frames:
dim(iris)
## [1] 150   5
nrow(unique(iris))
## [1] 149
unique(iris$Sepal.Width)
##  [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 2.9 3.7 4.0 4.4 3.8 3.3 4.1 4.2 2.3 2.8
## [18] 2.4 2.7 2.0 2.2 2.5 2.6
length(unique(iris$Sepal.Width))
## [1] 23
par(mfrow = c(1,2))
plot(iris$Sepal.Length, col = 'darkturquoise',type = 'b', frame.plot = F)
plot(unique(iris$Sepal.Length), col = 'darkviolet',type='b', frame.plot = F)

Saturday, October 14, 2017

var() {base}


var() function that computes the variation of the values in x.

var(x, y = NULL, na.rm = FALSE)

The arguments are:
  • x: numeric vector
  • y:NULL (default) or a vector, matrix or data frame with compatible dimensions to x.
  • na.rm: logical value indicating whether NA values should be removed before the computation proceeds.

Formula to calculate the Standard Deviation:
σ=(xix¯)2n1

The variance of a data set measures the mathematical dispersion of the data relative to the mean. However, this value is difficult to apply in a real-world sense because the values used to calculate it were squared.
The standard deviation, as the square root of the variance gives a value that is in the same units as the original values, which makes it much easier to work with and to interpret. (https://rfunctionaday.blogspot.com.es/2017/10/sd-base.html)

x:
x = c(1,6,10,23,4,5,56)
var1 = sum((x - mean(x))^2)/(length(x)-1)
var2 = var(x)
var1 ; var2 #same result
## [1] 378
## [1] 378
y:
var(1:10); var(1:10,1:10) #same result
## [1] 9.166667
## [1] 9.166667
y = c(10,45,10,3,24,54,5)
var1 = var(x); var3 = var(y)
var4 = var(x,y)
var1 ; var3 ; var4
## [1] 378
## [1] 415.619
## [1] -195
na.rm:
z = c(1,6,10,23,NA, NA, 4,5,56, 56, NA)
var3 = sum((z - mean(z))^2)/(length(z)-1)
var4 = var(z)
var5 = var(z, na.rm = TRUE) #remove NA values to compute var
var3 ; var4 ; var5
## [1] NA
## [1] NA
## [1] 534.125
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'])

varset = var(iris$Sepal.Length[iris$Species=='setosa'])
varversi = var(iris$Sepal.Length[iris$Species=='versicolor'])
varvir = var(iris$Sepal.Length[iris$Species=='virginica'])
plot(iris$Species, iris$Sepal.Length, col = 'thistle1', main = 'SD/Var')
#SD segments
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, 3, meanvir-sdvir, col = 'deeppink',lwd = 5)
#var segments
segments(1, meanset+varset,1, meanset-varset, col = 'darkviolet', lwd = 5)
segments(2, meanversi+ varversi, 2, meanversi-varversi, col = 'darkviolet', lwd = 5)
segments(3, meanvir + varvir, 3, meanvir-varvir, col = 'darkviolet',lwd = 5)

duplicated() {base}

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