# Extract First or Last n Characters from String in R (3 Examples) | Get Leading & Trailing Chars

In this tutorial, I will explain how to **extract n leading or trailing characters from a string** in R. I will structure the article as follows:

- Creation of Example Data
- Extract the First n Characters from String (Example 1)
- Extract the Last n Characters from String (Example 2)
- Extract the Last n Characters from String with the stringr Package (Example 3)
- Further Resources for the Handling of Characters in R

Let’s move directly to the R syntax…

## Create Example Character String

In the examples of this R tutorial, I will use the following character string:

x <- "this is an example" # Create example character string |

x <- "this is an example" # Create example character string

Our example string contains the sentence *“this is an example”*. Now let’s see how we can get the first and last n characters of this example string…

## Example 1: Extract First n Characters from String in R

In the first example, you will learn how to get the first n characters of a string. For this task, we can use the substr function:

substr(x, 1, 3) # Extract first three characters # "thi" |

substr(x, 1, 3) # Extract first three characters # "thi"

As you can see based on the previous R code, the substr function returned *thi* to the RStudio console (i.e. the first three characters).

In order to extract the first n characters with the substr command, we needed to specify three values within the function:

- The character string (in our case x).
- The first character we want to keep (in our case 1).
- The last character we want to keep (in this specific example we extracted the first 3 values).

Looks good! So what if we want to get the last n characters of our example data? That’s what you will learn in the next two examples…

## Example 2: Extract Last n Characters from String in Base R

In the second example, I will also use the substr function (as in Example 1). However, this time the code is getting a bit more complex:

n_last <- 3 # Specify number of characters to extract substr(x, nchar(x) - n_last + 1, nchar(x)) # Extract last three characters # "ple" |

n_last <- 3 # Specify number of characters to extract substr(x, nchar(x) - n_last + 1, nchar(x)) # Extract last three characters # "ple"

As you can see based on the previous output, we have extracted the characters *ple*, i.e. the last three characters of our example string.

In order to do that, we had to specify three inputs for the substr function again:

- The character string (in our case x).
- The first character we want to keep. For this part, we first had to use the nchar function to find out the length of our string (i.e.
*nchar(x)*). Then we had to subtract the amount of characters we want to extract from the length of our string (i.e.*nchar(x) – n_last*). Since we need to specify the starting character with the substr function, we had to add the value 1 to our calculation (i.e.*nchar(x) – n_last + 1*). - The last character we want to keep (in our case the last character of our string, i.e.
*nchar(x)*).

Works fine, but admittedly this code is more complicated as in Example 1. For that reason, I’m going to show you an easier solution for extracting the last n characters of a string in the next example…

## Example 3: Extract Last n Characters from String in R with the stringr Package

The stringr R package provides an easy way for getting the last n characters of a string. Let’s install and load the package first:

install.packages("stringr") # Install stringr package in R library("stringr") # Load stringr package |

install.packages("stringr") # Install stringr package in R library("stringr") # Load stringr package

Now we can use the str_sub function of the stringr package as follows:

str_sub(x, - 3, - 1) # Extract last characters with str_sub # "ple" |

str_sub(x, - 3, - 1) # Extract last characters with str_sub # "ple"

The same output as before with the substr function (i.e. *ple*), but this time with a much simpler R syntax. To get this output, we had to specify three inputs for the str_sub function:

- The character string (in our case x).
- The first character we want to keep (in our case – 3). Note: The str_sub function allows counting backward by putting a minus sign in front of the counting number.
- The last character we want to keep (in our case – 1, i.e. the last character of x).

For further explanations, you might also have a look at the R documentation of the str_sub command:

**Figure 1: str_sub Function Help Documentation.**

Much easier than the substr base R solution, if you ask me!

## Further Resources on the Handling of Strings

Dealing with strings in the R programming language can be tricky. If you want to improve your knowledge about the handling of strings in R, I can recommend the following video of the Tutorials Point YouTube channel. The speaker gives general advice how to deal with strings in R.

Furthermore, you could also have a look at the following articles and overviews of this website:

- substr & substring Functions in R
- str_sub R Function (stringr Package)
- The nchar R Function
- parse, deparse & expression Functions in R
- The R eval Function
- The R Programming Language

In this tutorial, you should have learned how to extract the first and the last n characters of a string in R. However, if you have further questions, don’t hesitate to let me know in the comments below!

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R