nth, first & last R Functions of dplyr Package (4 Examples)
The page will consist of four examples for the extraction of specific vector elements. To be more specific, the page looks as follows:
- Creation of Example Data
- Example 1: nth Function
- Example 2: nth Function with Negative Value
- Example 3: first Function
- Example 4: last Function
- Video & Further Resources
So now the part you have been waiting for – the examples:
Creation of Example Data
The examples of this R tutorial are based on the following vector:
x <- letters[1:10] # Create example vector x # "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
Our example vector is a character vector and contains ten letters.
In order to be able to use the nth, first, and last functions, we also need to install and load the dplyr package to R:
install.packages("dplyr") # Install & load dplyr library("dplyr")
Now, we can move on to the application of the functions…
Example 1: nth Function
Example 1 explains how to apply the nth function of the dplyr package. The nth function is used to extract a vector element somewhere in the middle of a vector.
Let’s assume that we want to extract the fifth element of our vector, then we can use the nth function as follows:
nth(x, 5) # Apply nth function # "e"
The RStudio console output is “e”, i.e. the fifth element of our example vector is the character “e”.
Example 2: nth Function with Negative Value
We can also use the nth function from the end of a vector. We simply have to put a minus sign in front of our position:
nth(x, - 3) # nth function with negative # "h"
The character “h” is the third last value of our example vector.
Example 3: first Function
The first function works similar as the nth function, but this R function returns just the first element of an input vector:
first(x) # Apply first function # "a"
The first element of our vector is “a”.
Example 4: last Function
The last command does the opposite as the first function: It returns the last element of a vector to the RStudio console:
last(x) # Apply last function # "j"
The character “j” is the last entry of our vector.
Video & Further Resources
Do you need more info on the dplyr package? Then you may have a look at the following video of my YouTube channel. I’m explaining other commands of the dplyr package in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read some of the other tutorials of this website:
- substr & substring Functions in R
- Extract First or Last n Characters from String with Base R
- str_extract Function of stringr Package
- dplyr R Package
- R Functions List (+ Examples)
- The R Programming Language
To summarize: In this R tutorial you learned how to get the first, last, or nth value from a vector or array. In case you have any additional comments or questions, please let me know in the comments section.