Sunday, August 6, 2017

head() tail() {base}


head() and tail() functions:

head() function displays the first values from a data frame, matrix or vector, while tail() function displays the last values.

Parameters:
 x:object to display the values from.
 n:number of values to be displayed, default values is 6.

#head() and tail() functions with a vector
head(1:20) 
## [1] 1 2 3 4 5 6
head(1:20, n = 10)   #10 first rows in the vector
##  [1]  1  2  3  4  5  6  7  8  9 10
head(1:20, n = -10)  #all the rows in the vector but the LAST 10!!
##  [1]  1  2  3  4  5  6  7  8  9 10
tail(1:20)
## [1] 15 16 17 18 19 20
tail(1:20, n= 10)     #10 first rows
##  [1] 11 12 13 14 15 16 17 18 19 20
tail(1:20, n= -5)     ##all the rows in the vector but the FIRST 10!!
##  [1]  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
#head() and tail() functions with a matrix
A <- head(matrix(1:50, ncol = 5), n = 3 ) #assign a name to the result 
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   11   21   31   41
## [2,]    2   12   22   32   42
## [3,]    3   13   23   33   43
A <- head(matrix(1:50, ncol = 5), n = -3 ) 
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   11   21   31   41
## [2,]    2   12   22   32   42
## [3,]    3   13   23   33   43
## [4,]    4   14   24   34   44
## [5,]    5   15   25   35   45
## [6,]    6   16   26   36   46
## [7,]    7   17   27   37   47
B <- tail(matrix(1:50, ncol = 5), n = 3 ) 
B
##       [,1] [,2] [,3] [,4] [,5]
##  [8,]    8   18   28   38   48
##  [9,]    9   19   29   39   49
## [10,]   10   20   30   40   50
B <- tail(matrix(1:50, ncol = 5), n = -3 ) 
B
##       [,1] [,2] [,3] [,4] [,5]
##  [4,]    4   14   24   34   44
##  [5,]    5   15   25   35   45
##  [6,]    6   16   26   36   46
##  [7,]    7   17   27   37   47
##  [8,]    8   18   28   38   48
##  [9,]    9   19   29   39   49
## [10,]   10   20   30   40   50
#head() and tail() functions with a matrix
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
head(iris, 10)
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1           5.1         3.5          1.4         0.2  setosa
## 2           4.9         3.0          1.4         0.2  setosa
## 3           4.7         3.2          1.3         0.2  setosa
## 4           4.6         3.1          1.5         0.2  setosa
## 5           5.0         3.6          1.4         0.2  setosa
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa
head(iris, -140)
##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1           5.1         3.5          1.4         0.2  setosa
## 2           4.9         3.0          1.4         0.2  setosa
## 3           4.7         3.2          1.3         0.2  setosa
## 4           4.6         3.1          1.5         0.2  setosa
## 5           5.0         3.6          1.4         0.2  setosa
## 6           5.4         3.9          1.7         0.4  setosa
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa
tail(iris)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
tail(iris, 10)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 141          6.7         3.1          5.6         2.4 virginica
## 142          6.9         3.1          5.1         2.3 virginica
## 143          5.8         2.7          5.1         1.9 virginica
## 144          6.8         3.2          5.9         2.3 virginica
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
#we can use it also inside other functions:
summary(head(iris))
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width    
##  Min.   :4.600   Min.   :3.000   Min.   :1.300   Min.   :0.2000  
##  1st Qu.:4.750   1st Qu.:3.125   1st Qu.:1.400   1st Qu.:0.2000  
##  Median :4.950   Median :3.350   Median :1.400   Median :0.2000  
##  Mean   :4.950   Mean   :3.383   Mean   :1.450   Mean   :0.2333  
##  3rd Qu.:5.075   3rd Qu.:3.575   3rd Qu.:1.475   3rd Qu.:0.2000  
##  Max.   :5.400   Max.   :3.900   Max.   :1.700   Max.   :0.4000  
##        Species 
##  setosa    :6  
##  versicolor:0  
##  virginica :0  
##                
##                
## 
summary(head(iris, 20))
##   Sepal.Length    Sepal.Width    Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.90   Min.   :1.100   Min.   :0.100  
##  1st Qu.:4.775   1st Qu.:3.10   1st Qu.:1.400   1st Qu.:0.200  
##  Median :5.000   Median :3.45   Median :1.400   Median :0.200  
##  Mean   :5.035   Mean   :3.48   Mean   :1.435   Mean   :0.235  
##  3rd Qu.:5.400   3rd Qu.:3.80   3rd Qu.:1.500   3rd Qu.:0.300  
##  Max.   :5.800   Max.   :4.40   Max.   :1.700   Max.   :0.400  
##        Species  
##  setosa    :20  
##  versicolor: 0  
##  virginica : 0  
##                 
##                 
## 
mean(head(iris$Sepal.Length,10))
## [1] 4.86
mean(head(iris$Sepal.Length,50))
## [1] 5.006

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