# Define a string
<- "Hello, world!"
my_string
# Extract the last 6 characters
<- substr(my_string, nchar(my_string) - 5, nchar(my_string))
substring_from_end
# Print the result
print(substring_from_end)
[1] "world!"
Steven P. Sanderson II, MPH
July 16, 2024
Hey useR’s! Today, we’re going to discuss a neat trick: extracting substrings starting from the end of a string. We’ll cover how to achieve this using base R, stringr
, and stringi
. By the end of this post, you’ll have several tools in your R toolbox for string manipulation. Let’s get started!
First up, let’s use base R functions to extract substrings from the end of a string. The substr
function is your friend here.
Here’s a simple example:
# Define a string
my_string <- "Hello, world!"
# Extract the last 6 characters
substring_from_end <- substr(my_string, nchar(my_string) - 5, nchar(my_string))
# Print the result
print(substring_from_end)
[1] "world!"
Explanation:
nchar(my_string)
returns the total number of characters in my_string
.nchar(my_string) - 5
calculates the starting position of the substring, counting from the end.substr(my_string, start, stop)
extracts the substring from the start position to the stop position.stringr
The stringr
package makes string manipulation more straightforward and readable. We’ll use the str_sub
function for this task.
First, install and load the stringr
package if you haven’t already:
Now, let’s extract a substring from the end:
# Define a string
my_string <- "Hello, world!"
# Extract the last 6 characters using stringr
substring_from_end <- str_sub(my_string, -6, -1)
# Print the result
print(substring_from_end)
[1] "world!"
Explanation:
str_sub(my_string, start, end)
extracts the substring from the start to the end position.str_sub
count from the end of the string. So -6
refers to the sixth character from the end, and -1
refers to the last character.stringi
The stringi
package is another powerful tool for string manipulation. We’ll use the stri_sub
function here.
First, install and load the stringi
package:
Let’s extract our substring:
# Define a string
my_string <- "Hello, world!"
# Extract the last 6 characters using stringi
substring_from_end <- stri_sub(my_string, from = -6, to = -1)
# Print the result
print(substring_from_end)
[1] "world!"
Explanation:
stri_sub(my_string, from, to)
works similarly to str_sub
, using from
and to
parameters to define the start and end positions.Now it’s your turn! Try these methods on your own strings. Here are a few ideas to get you started:
We’ve explored how to extract substrings from the end of a string using base R, stringr
, and stringi
. Each method has its own charm, so choose the one that fits your coding style best. String manipulation is a crucial skill in data cleaning and text analysis, so keep practicing and experimenting.
Happy coding! 🚀