# Indexing Up to the End of Vector & Data Frame in R (2 Examples)

In this article youâ€™ll learn how to return the end of a vector or data frame in R programming.

Table of contents:

Letâ€™s take a look at some R codes in actionâ€¦

## Example 1: Return End of Vector

In Example 1, Iâ€™ll explain how to extract the last part of a vector or array in R. First, we have to create an example vector:

```vec <- 1:10                      # Example vector
vec                              # Print vector
# 1  2  3  4  5  6  7  8  9 10```

Now, we can subset our vector by specifying the index positions we want to remove with a minus sign in front:

```vec[- (1:7)]                     # Return last elements
# 8  9 10```

As you can see based on the previous output of the RStudio console, we kept only the last three elements.

## Example 2: Return End of Data Frame

The following R syntax shows how to keep only the bottom part of a data frame in R. Again, we first need to create some example data:

```data <- data.frame(x1 = 1:10,    # Example data
x2 = letters[1:10])
data                             # Print data
#    x1 x2
# 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```

Now, we can extract the bottom part of the input matrix using the tail function:

```tail(data, 3)                    # Return last rows
#    x1 x2
# 8   8  h
# 9   9  i
# 10 10  j```

As you can see, we kept only the bottom three rows.

## Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which illustrates the content of this tutorial. You can find the video below.

The YouTube video will be added soon.

In addition, you may want to have a look at the other articles of https://statisticsglobe.com/. You can find a selection of tutorials below.

Summary: This article illustrated how to extract the end of data frames and arrays in R programming. Please tell me about it in the comments below, in case you have further questions.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Top