head and tail functions in R

R introduction Structure exploration

The `head` and `tail` functions allow to obtain the first or last parts of vectors, matrices, tables, data frames or functions. By default, only the first or last six elements are returned, but you can also specify the desired number of elements.

The basic usage of the `head` command in R is to retrieve the first elements of a vector or list or the first rows of a data frame or matrix. By default, the function will return the first six elements or rows, as shown in the examples below:

If you have a vector `x` the `head` function will return its six first elements.

``````x <- c(10, 12, 20, 25, 31, 17, 7, 3 ,19)

``10 12 20 25 31 17``

If your data is a data frame named `df` the `head` function will list its first six rows.

``````df <- data.frame(x = 1:10, y = LETTERS[1:10])

``````  x y
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
6 6 F``````

You can also specify the number of parts to be returned from the `head` function making use of the `n` argument. In the following example we are returning only the first two elements of the list named `l`.

``````l <- list(a = 1, b = 2, c = 3)

``````\$a
[1] 1

\$b
[1] 2``````

However, the argument `n` can also take negative numbers to specify how many elements to remove starting from the bottom. Going back to our fist example, if you want all the elements except the last two you can do:

``````x <- c(10, 12, 20, 25, 31, 17, 7, 3, 19)

``10 12 20 25 31 17  7``

Filtering first rows and columns

If the dimensions of the object passed to the function are not `NULL` you can also specify the number of first columns to return passing a vector with as many numbers as dimensions to `n`. For instance, if you want to view the first ten rows and first two columns of a data frame you can type:

``````df2 <- data.frame(x = 1:26, y = letters, z = LETTERS)

``````    x y
1   1 a
2   2 b
3   3 c
4   4 d
5   5 e
6   6 f
7   7 g
8   8 h
9   9 i
10 10 j``````

The `tail` function

The `tail` command is very similar but the opposite of the `head` function, as it will return the last elements of a vector or list or the last rows of a data frame or matrix.

If you apply the `tail` function to a vector you will retrieve its last six elements:

``````x <- c(10, 12, 20, 25, 31, 17, 7, 3 ,19)

tail(x)``````
``25 31 17  7  3 19``

If you use the function with a data frame or matrix you will get the last six rows by default:

``````df <- data.frame(x = 1:10, y = LETTERS[1:10])

tail(df)``````
``````    x y
5   5 E
6   6 F
7   7 G
8   8 H
9   9 I
10 10 J``````

You can also specify the desired number of last elements to return with `n`. In the example below we are getting the last two rows of the data frame.

``````df <- data.frame(x = 1:10, y = LETTERS[1:10])

tail(df, n = 2)``````
``````    x y
9   9 I
10 10 J``````

Recall that you can also specify negative numbers, which will remove the first `n` rows or elements.

``````df <- data.frame(x = 1:10, y = LETTERS[1:10])

tail(df, n = -1)``````
``````    x y
2   2 B
3   3 C
4   4 D
5   5 E
6   6 F
7   7 G
8   8 H
9   9 I
10 10 J``````