Extract First & Last Word of Character String in R (3 Examples)
In this R tutorial you’ll learn how to get the first and last word of a character string.
The post is structured as follows:
Let’s get started:
Creation of Example Data
The following data will be used as a basis for this R programming language tutorial:
my_string <- "have a look at my very nice character string" # Create example string my_string # Print example string # [1] "have a look at my very nice character string"
Have a look at the previous output of the RStudio console. It visualizes that our example data is a single character string containing multiple different words. These words a re separated by blanks.
Example 1: Get Last Word of Character String Using sub() Function
In this example, I’ll explain how to extract the very last word of a character string using the basic installation of the R programming language.
To accomplish this, we can apply the sub function as shown below:
my_string_last1 <- sub('^.* ([[:alnum:]]+)$', '\1', # Extract last word my_string) my_string_last1 # Print last word # [1] "string"
As you can see, the previous R code has printed the character string “string”, i.e. the last word of our input character string.
Example 2: Get Last Word of Character String Using word() Function of stringr Package
Example 2 shows how to use the stringr package to return the last word of a character string.
In order to use the functions of the stringr package, we first need to install and load stringr:
install.packages("stringr") # Install & load stringr library("stringr")
Next, we can apply the word() function of the stringr package:
my_string_last2 <- word(my_string, - 1) # Extract last word my_string_last2 # Print last word # [1] "string"
As you can see, the word() function has returned the same output as Base R in Example 1. However, the R syntax was much simpler.
Example 3: Get First Word of Character String Using word() Function of stringr Package
Similar to Example 2, we can use the word() function to find the first word of a character string.
Consider the R programming code below:
my_string_first <- word(my_string, 1) # Extract first word my_string_first # Print first word # [1] "have"
The first word of our character string is “have”.
Video, Further Resources & Summary
Do you need further information on the R programming syntax of this article? Then I recommend having a look at the following video on my YouTube channel. I’m explaining the contents of this article in the video.
Furthermore, you may have a look at some of the related posts which I have published on this homepage. I have released numerous articles on topics such as extracting data, character strings, and numeric values already:
- Extract Numbers from Character String Vector
- Extract First or Last n Characters from String in R
- Extract First Entry from Character String Split
- Capitalize First Letter of Each Word in Character String
- All R Programming Examples
In summary: In this tutorial, I have demonstrated how to return the first and last word of a character string in the R programming language. Please let me know in the comments below, in case you have additional questions.