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

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:   ...