Monday, September 25, 2017

cut() {base}


cut() function divides the range of x into intervals. The values are coded according to which interval they fall. The leftmost interval corresponds to level one, the next leftmost to level two and so on.

cut(x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, dig.lab = 3, ordered_result = FALSE, ...)

The parameters are:
 -x: a numeric vector to be converted into a factor 
breaks: a numeric vector giving the cutting points or a single number giving the number of intervals 
labels: labels to be used for the levels created. If labels = FALSE an integer code is returned 
include.lowest: logical, if the lowest value is included in the interval (or the highest, when right = FALSE)
 - right: logical, indicates if the intervals should be close on the right or on the left 
dig.lab: integer, number of digits used when labels are not given
 - ordered_result:logical, indicates if the result should be shown as ordered factor

head(women)
##   height weight
## 1     58    115
## 2     59    117
## 3     60    120
## 4     61    123
## 5     62    126
## 6     63    129
summary(women)
##      height         weight     
##  Min.   :58.0   Min.   :115.0  
##  1st Qu.:61.5   1st Qu.:124.5  
##  Median :65.0   Median :135.0  
##  Mean   :65.0   Mean   :136.7  
##  3rd Qu.:68.5   3rd Qu.:148.0  
##  Max.   :72.0   Max.   :164.0

breaks:
cut(women$height, breaks = c(50,60,70,80)) #numeric vector giving the cutting points
##  [1] (50,60] (50,60] (50,60] (60,70] (60,70] (60,70] (60,70] (60,70]
##  [9] (60,70] (60,70] (60,70] (60,70] (60,70] (70,80] (70,80]
## Levels: (50,60] (60,70] (70,80]
cut(women$height, breaks = 3) #number of intervals  
##  [1] (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: (58,62.7] (62.7,67.3] (67.3,72]

labels:
cut(women$height, c(50,60,70,80)) #no labels
##  [1] (50,60] (50,60] (50,60] (60,70] (60,70] (60,70] (60,70] (60,70]
##  [9] (60,70] (60,70] (60,70] (60,70] (60,70] (70,80] (70,80]
## Levels: (50,60] (60,70] (70,80]
cut(women$height, c(50,60,70,80), labels = c(1,2,3)) #with labels
##  [1] 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
## Levels: 1 2 3
cut(women$height, breaks = 3, labels = c("Short", "Medium", "Tall")) #with labels
##  [1] Short  Short  Short  Short  Short  Medium Medium Medium Medium Medium
## [11] Tall   Tall   Tall   Tall   Tall  
## Levels: Short Medium Tall
cut(women$height, c(50,60,70,80), labels = FALSE)  #labels = FALSE, integer code is returned
##  [1] 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3

include.lowest:
cut(women$height, breaks = 3, include.lowest = TRUE)
##  [1] [58,62.7]   [58,62.7]   [58,62.7]   [58,62.7]   [58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: [58,62.7] (62.7,67.3] (67.3,72]
cut(women$height, breaks = 3, include.lowest = FALSE)
##  [1] (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: (58,62.7] (62.7,67.3] (67.3,72]

right:
#indicates if intervals are closed on the right or left
cut(women$height, breaks = 3, right = TRUE) #closed on the right
##  [1] (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: (58,62.7] (62.7,67.3] (67.3,72]
cut(women$height, breaks = 3, right = FALSE) #closed on the left
##  [1] [58,62.7)   [58,62.7)   [58,62.7)   [58,62.7)   [58,62.7)  
##  [6] [62.7,67.3) [62.7,67.3) [62.7,67.3) [62.7,67.3) [62.7,67.3)
## [11] [67.3,72)   [67.3,72)   [67.3,72)   [67.3,72)   [67.3,72)  
## Levels: [58,62.7) [62.7,67.3) [67.3,72)
cut(women$height, breaks = 3, right = FALSE, include.lowest = TRUE)
##  [1] [58,62.7)   [58,62.7)   [58,62.7)   [58,62.7)   [58,62.7)  
##  [6] [62.7,67.3) [62.7,67.3) [62.7,67.3) [62.7,67.3) [62.7,67.3)
## [11] [67.3,72]   [67.3,72]   [67.3,72]   [67.3,72]   [67.3,72]  
## Levels: [58,62.7) [62.7,67.3) [67.3,72]

dig.lab:
#number of digits used when labels are not given
cut(women$height, breaks = 3, dig.lab = 1)
##  [1] (58,63] (58,63] (58,63] (58,63] (58,63] (63,67] (63,67] (63,67]
##  [9] (63,67] (63,67] (67,72] (67,72] (67,72] (67,72] (67,72]
## Levels: (58,63] (63,67] (67,72]
cut(women$height, breaks = 3, dig.lab = 5)
##  [1] (57.986,62.667] (57.986,62.667] (57.986,62.667] (57.986,62.667]
##  [5] (57.986,62.667] (62.667,67.333] (62.667,67.333] (62.667,67.333]
##  [9] (62.667,67.333] (62.667,67.333] (67.333,72.014] (67.333,72.014]
## [13] (67.333,72.014] (67.333,72.014] (67.333,72.014]
## Levels: (57.986,62.667] (62.667,67.333] (67.333,72.014]

ordered_result:
cut(women$height, breaks = 3, ordered_result = FALSE) #levels are not ordered
##  [1] (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: (58,62.7] (62.7,67.3] (67.3,72]
cut(women$height, breaks = 3, ordered_result = TRUE) #levels are ordered
##  [1] (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]   (58,62.7]  
##  [6] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3] (62.7,67.3]
## [11] (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]   (67.3,72]  
## Levels: (58,62.7] < (62.7,67.3] < (67.3,72]
women$factorheight = cut(women$height, breaks = 3, labels = c("Short", "Medium", "Tall"))
par(mfrow = c(1,2))
plot(women$height, women$weight, col = 'deeppink', type = "b", ylab = 'Weight', xlab = 'Height (C.Variable)' ) #height as continuos variable
plot(women$factorheight, women$weight, col = c('gold', 'darkslategray1', 'violet'), ylab = 'Weight', xlab = 'Height (Factor)') #height as factor

Friday, September 22, 2017

tolower() toupper() {base}


tolower() and toupper() functions translate character vectors from upper to lower case, and from lower to upper case, respectively.

tolower(x) 
toupper(x)

The parameters are:
 - x: a character vector

tolower():
x = 'abcd, abcDEF, ABCDDEE, 1234'
x
## [1] "abcd, abcDEF, ABCDDEE, 1234"
tolower(x)
## [1] "abcd, abcdef, abcddee, 1234"
All the characters have been converted to lower case, and the numeric objects have been converted to characters.

toupper(x):
x
## [1] "abcd, abcDEF, ABCDDEE, 1234"
toupper(x)
## [1] "ABCD, ABCDEF, ABCDDEE, 1234"
All the characters have been converted to upper case, and the numeric objects have been converted to characters and have left unchanged.
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6
par(mfrow= c(1,2))
boxplot(airquality[1:4], las = 2, col = c('aquamarine', 'aquamarine2', 'aquamarine3', 'aquamarine4'))
boxplot(airquality[1:4], names = c(toupper(colnames(airquality[1:4]))), las = 2, col = c('violet', 'violetred1', 'violetred3', 'violetred4'))

Function to capitalized just the first letter:
#Example capitalizing first letter of word 'hello':
paste(toupper(substring('hello', 1, 1)), substring('hello', 2),
          sep = "", collapse = " ")
## [1] "Hello"
#Function:
simpleCap =function(x) {
    s =strsplit(x, " ")[[1]]
    paste(toupper(substring(s, 1, 1)), substring(s, 2),
          sep = "", collapse = " ")}

y = 'hi, how are you?'
z = c('abcd', 'bbddee', 1234)
simpleCap(y) 
## [1] "Hi, How Are You?"
simpleCap(z) #the function just capitalized the first element in a list, and z is a vector of elements. Just the first element will be used.
## [1] "Abcd"
par(mfrow = c(1,3), oma=c(0,0,2,0))
boxplot(weight ~ feed, data = chickwts,ylab = "Weight at six weeks (gm)", las =2, col = topo.colors(6, 0.5))
boxplot(weight ~ feed, data = chickwts, names = toupper(levels(chickwts$feed)), ylab = "Weight at six weeks (gm)", las = 2, col = cm.colors(6))
boxplot(weight ~ feed, data = chickwts, names = sapply(levels(chickwts$feed), simpleCap),ylab = "Weight at six weeks (gm)", las = 2, col=colors(6))
title(main = 'Diferences in the labels using `chickwt` dataset', outer = TRUE)

Thursday, September 21, 2017

round() signif() {base}


round() function rounds the values in its first argument to the specified number of decimal places (default 0).

signif() function rounds the values in its first argument to the specified number of significant digits.

round(x, digits = 0) 
signif(x, digits = 6)

The parameters are:
 -x: numeric vector
 -digits: integer to indicate the number of decimal places or significant digits.

Round():
x = c(1.2346578, 1.24367, 1.78511, 1.536)
round(x,2)
## [1] 1.23 1.24 1.79 1.54
The numbers are rounded to have two decimal places. In the third and fouth place we see that is has been rounded up due to the following decimals in the original numbers.
round(x,3)
## [1] 1.235 1.244 1.785 1.536
In this case, the first and second numbers have been rounded up.
y = c(1.2346578, 11.24367, 111.78511, 1111.536)
round(y,-1) #rounds the numbers previous to the decimal places
## [1]    0   10  110 1110
round(y,-2)
## [1]    0    0  100 1100
z = c(123.23, 245.24, 667.78, 664.53)
round(z,-1) #rounds the numbers previous to the decimal places.
## [1] 120 250 670 660
round(z,-2)
## [1] 100 200 700 700
We can see that some numbers have been rounded up, while others have been rounded down.

Signif():
While round() rounds the values to a specific number of decimal places signif() rounds the value to sigfinicant digits (all the places of the number, not just decimal places).
r = c(1.2346578, 11.24367, 111.78789, 1111.16789)
r
## [1]    1.234658   11.243670  111.787890 1111.167890
round(r,2)
## [1]    1.23   11.24  111.79 1111.17
signif(r,2)
## [1]    1.2   11.0  110.0 1100.0
round(r,5)
## [1]    1.23466   11.24367  111.78789 1111.16789
signif(r,5)
## [1]    1.2347   11.2440  111.7900 1111.2000

Plot round() and signif() using the variable Sepal.Length from the iris dataset:
NoFunction = summary(iris$Sepal.Length)
Round = round(summary(iris$Sepal.Length), 1)
Signif = signif(summary(iris$Sepal.Length), 1)
NoFunction
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   4.300   5.100   5.800   5.843   6.400   7.900
Round
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     4.3     5.1     5.8     5.8     6.4     7.9
Signif
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       4       5       6       6       6       8
boxplot(NoFunction,Round,Signif, col = c('darksalmon', 'salmon', 'darkred'), names = c('NoFunction', 'Round', 'Signif'), ylab= 'iris$Sepal.Length')

duplicated() {base}

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