Skip to main content

head() and tail() functions in R


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

Popular posts from this blog

Support Vector Machines (SVM) in R (package 'kernlab')

Support Vector Machines (SVM) learning combines of both the instance-based nearest neighbor algorithm and the linear regression modeling. Support Vector Machines can be imagined as a surface that creates a boundary (hyperplane) between points of data plotted in multidimensional that represents examples and their feature values. Since it is likely that the line that leads to the greatest separation will generalize the best to the future data, SVM involves a search for the Maximum Margin Hyperplane (MMH) that creates the greatest separation between the 2 classes. If the data ara not linearly separable is used a slack variable, which creates a soft margin that allows some points to fall on the incorrect side of the margin. But, in many real-world applications, the relationship between variables are nonlinear. A key featureof the SVMs are their ability to map the problem to a higher dimension space using a process known as the Kernel trick, this involves a process of constructing ne...

Initial Data Analysis (infert dataset)

Initial analysis is a very important step that should always be performed prior to analysing the data we are working with. The data we receive most of the time is messy and may contain mistakes that can lead us to wrong conclusions. Here we will use the dataset infert , that is already present in R. To get to know the data is very important to know the background and the meaning of each variable present in the dataset. Since infert is a dataset in R we can get information about the data using the following code: require(datasets) ?infert #gives us important info about the dataset inf <- infert #renamed dataset as 'inf' This gives us the following information: Format 1.Education: 0 = 0-5 years, 1 = 6-11 years, 2 = 12+ years 2.Age: Age in years of case 3.Parity: Count 4.Number of prior induced abortions: 0 = 0, 1 = 1, 2 = 2 or more 5.Case status: 1 = case 0 = control 6.Number of prior spontaneous abortions: 0 = 0, 1 = 1, 2...

Ant Colony Optimization (part 2) : Graph optimization using ACO

The Travelling Salesman Problem (TSP) is one of the most famous problems in computer science for studying optimization, the objective is to find a complete route that connects all the nodes of a network, visiting them only once and returning to the starting point while minimizing the total distance of the route. The problem of the traveling agent has an important variation, and this depends on whether the distances between one node and another are symmetric or not, that is, that the distance between A and B is equal to the distance between B and A, since in practice is very unlikely to be so. The number of possible routes in a network is determined by the equation: (𝒏−𝟏)! This means that in a network of 5 nodes the number of probable routes is equal to (5-1)! = 24, and as the number of nodes increases, the number of possible routes grows factorially. In the case that the problem is symmetrical the number of possible routes is reduced to half: ( (𝒏−𝟏)! ) / 𝟐 The complexity o...