You can use the following basic syntax in dplyr to remove the first character from each string in a particular column:
library(dplyr) df_new <- df %>% mutate(across(c('my_column'), substr, 2, nchar(my_column)))
This particular syntax removes the first character from each string in the column called my_column.
Note that we use the substr() function to extract the substring ranging from the second character in each string to the length of the string.
This has the effect of removing the first character from the string.
The following example shows how to use this syntax in practice.
Example: Remove First Character from Strings Using dplyr
Suppose we have the following data frame in R:
#create data frame df <- data.frame(team=c('XMavs', 'XPacers', 'XHawks', 'XKings', 'XNets', 'XCeltics'), points=c(104, 110, 134, 125, 114, 124)) #view data frame df team points 1 XMavs 104 2 XPacers 110 3 XHawks 134 4 XKings 125 5 XNets 114 6 XCeltics 124
Suppose we would like to remove the first character from each string in the team column.
We can use the following syntax to do so:
library(dplyr) #remove first character from each string in 'team' column df_new <- df %>% mutate(across(c('team'), substr, 2, nchar(team))) #view updated data frame df_new team points 1 Mavs 104 2 Pacers 110 3 Hawks 134 4 Kings 125 5 Nets 114 6 Celtics 124
Notice that the first character of each string in the team column has been removed.
Note that the nchar() function is used to calculate the total number of characters in a string.
Thus, we use the substr() function to extract the substring ranging from the second character to the last character in each string, which is equivalent to removing the first character from each string.
Note: If you’d like to remove the first character from strings in multiple columns, simply include multiple column names in the across() function.
Additional Resources
The following tutorials explain how to perform other common tasks in dplyr:
How to Remove Rows Using dplyr
How to Select Columns by Index Using dplyr
How to Filter Rows that Contain a Certain String Using dplyr
Hello! hope you are well!
Just a small comment. I had the same code in previous work, and I needed to repeat it for a new project.
For some reason when I run it, it doesn’t do anything anymore.
I tested your code to ensure I was not going mad, and is the same?
Do you have the same problem? I had used substr() on its own and works fine…
Any idea would be useful!
Many thanks
Bea
Hi Beatriz…It’s likely not that `dplyr` itself stopped working for string slicing, but rather that something in your environment changed that’s affecting how the code is executed.
A few possibilities come to mind:
1. String column type changed
If your column is now a factor rather than a character vector, `substr()` will work differently (or not at all) inside a `mutate()` pipeline. Check with:
“`r
str(df)
“`
If it’s a factor, convert first:
“`r
df <- df %>% mutate(col = as.character(col))
“`
2. Column name mismatch
If you renamed columns or read in a slightly different dataset, the pipeline may be referencing the wrong column.
3. dplyr masking base R functions
Normally, `substr()` works fine inside `mutate()`, but if another package masks it or if you’ve attached packages in a different order, you might be getting a different function. Try explicitly calling:
“`r
df %>% mutate(col = base::substr(col, 2, nchar(col)))
“`
4. Different version of dplyr or R
If you upgraded R or `dplyr`, some subtle evaluation differences might appear — especially if your old code relied on non-standard evaluation quirks. Using `stringr::str_sub()` is often more reliable:
“`r
library(stringr)
df %>% mutate(col = str_sub(col, 2))
“`