How to Use the tail() Function in R


Often you may want to return only the last n rows from a data frame in R.

The easiest way to do so is by using the tail() function from base R, which can be used to perform this exact task.

The tail() function uses the following syntax:

tail(x, n)

where:

  • x: Name of data frame
  • n: Number of rows to return from the bottom of the data frame

Note: If you don’t specify a number for the n argument then the tail() function will return the last six rows of the data frame by default. This is similar to how the head() function will return the first six rows of a data frame by default.

The following example shows how to use the tail() function in practice with a data frame in R.

Example: How to Use the tail() Function in R

Suppose that we create the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 31, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Suppose that we would like to extract only the last six rows of the data frame.

We can use the following syntax with the tail() function to do so:

#extract last six rows of data frame
tail(df)

  team points assists rebounds
3    A     86      31       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

We can see that the tail() function returns the last six rows of the data frame.

To return a different number of rows from the end of the data frame, simply specify a number for the n argument.

For example, we can use the following syntax to return only the last three rows from the data frame:

#extract last three rows of data frame
tail(df, n=3)

  team points assists rebounds
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Notice that only the last three rows are returned, just as we specified.

By default, the tail() function will return each column in the data frame. However, you can subset the data frame to only return specific columns if you’d like.

For example, we can use the following syntax to return the last three rows only for the team and points columns of the data frame:

#extract last three rows of data frame for team and points columns only
tail(df[c('team', 'points')], n=3)

  team points
6    B     74
7    B     78
8    B     93

Notice that only the last three rows are returned for the team and points column of the data frame.

In practice, you will likely use the tail() function when you are interested in understanding what the last few rows of a data frame look like and to check if there are any missing values that occur at the end of the data frame due to data entry error or some other reason.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Frequency Table by Group in R
How to Create a Frequency Polygon in R
How to Create Relative Frequency Tables in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *