• Post author:
• Post category:R Programming

How to access the last value in an R vector? You can use the length() function to access the last value in a vector. Vectors are a common data structure in R programming language that can store values of common data types. In this article, I will explain various methods of R to access the last value in a vector with multiple examples.

Key points-

• The `length()` function returns the number of elements in a vector, allowing you to index the last value directly.
• The `tail()` function is a convenient way to obtain the last value of a vector by specifying the number of elements to return.
• Indexing with `length` is another straightforward method to access the last element.
• By reversing the vector with the `rev()` function and accessing the first element, you can retrieve the last value.
• The `dplyr` package provides the `last()` function, which simplifies extracting the last element from a vector.
• The `subset()` function can be used with `length` to filter and obtain the last value.
• The `which.max()` function, applied to the vector sequence, allows identification of the last element.

## Using the length() function

Let’s create a character vector using the `c()` function in R, which combines values into a vector. Next, use the `length()` function to get the length of the vector, which corresponds to the position of its last element. Finally, use this position to access the last value in the vector.

``````
# Get last value of vector using length()
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- vec[length(vec)]
print("Get last value from the vector:")
print(last_value)
``````

Yields below output.

## Using tail() function

In R, the `tail()` function is used to retrieve the last value of a vector. To do this, set the `n` parameter to `1` and pass it into this function along with the vector. This will return the last value of the specified vector.

``````
# Get last value of vector using tail()
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- tail(vec, n=1)
print("Get the last value from the vector:")
print(last_value)
``````

Yields the same as the above output.

## Using reverse indexing

Alternatively, you can use the `rev()` function to get the last value present in the vector. This function reverses the order of the values in the vector. After reversing the vector, you can use square bracket notation `[1]` to access the first element of this reversed vector.

``````
# Get last value of vector using rev()
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- rev(vec)[1]
print("Get the last value from the vector:")
print(last_value)
``````

This syntax `rev(vec)[1]` retrieves the first element of the reversed vector, which is `Examples`.

Yields the same as the above output.

## Using dplyr package

You can utilize the `last()` function from the `dplyr` package to easily access the last value of a vector. When using functions from the `dplyr` package, installing the package in your R environment is important. Once installed, load the package using `library(dplyr)`.

The `last()` function specifically returns the last value of a vector. To retrieve this value, you simply pass the vector as an argument to the `last()` function.

``````
# Get last value of vector using dplyr
library(dplyr)
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- last(vec)
print("Get the last value from the vector:")
print(last_value)
``````

Yields the same as the above output.

## Using subset() with length

In this example, you can use the `subset()` function along with `length()` to access the last value of a vector. Initially, you pass the `seq_along(vec)` function,(which generates a sequence of indices corresponding to the length of the vector) to the `subset()` function along with the vector itself. This approach retrieves a subset of the vector that includes the last value, effectively returning the last element of the vector.

``````
# Get last value of vector using subset()
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- subset(vec, seq_along(vec) == length(vec))
print("Get the last value from the vector:")
print(last_value)
``````

Yields the same as the above output.

## Using which.max() Function

Finally, you can use the `which.max()` function along with the `seq_along( )` function to access the last value present in the vector. First, you can use the seq_along() function to generate a sequence of indices corresponding to the length of the vector and then apply `which.max()` to identify the maximum value within that sequence. This maximum value corresponds to the index of the last element in the vector, which you can then access directly to obtain the last value.

``````
# Get last value of vector using which.max()
vec <- c("Spark", "By", "Examples")
print("Given vector")
print(vec)
last_value <- vec[which.max(seq_along(vec))]
print("Get the last value from the vector:")
print(last_value)
``````

Yields the same as the above output.

## Conclusion

In this article, I have explained various approaches of R to access the last value in a vector, such as base R functions and additional packages like `dplyr` with well-defined examples.