Vibramat
Vibramat

Reputation: 217

Remove a number of character from string in a column

I have a data frame with a column of strings and I would like to remove the first three characters in each of the strings. As in the following example:

From this:

df <- data_frame(col1 = c('01_A','02_B', '03_C'))

To this:

df <- data_frame(col1 = c('A','B', 'C'))

I have been trying to use the dplyr transmute function but I can't really get it to work.

Any help would be super appreciated!

Upvotes: 0

Views: 835

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101247

You can use sub like below

> df %>%
+   mutate(col1 = sub("^.{3}", "", col1))
# A tibble: 3 x 1
  col1
  <chr>
1 A
2 B
3 C

Upvotes: 0

akrun
akrun

Reputation: 887068

We could also use substring from base R as the OP mentioned above position based substring extraction

df$col1 <- substring(df$col1, 4)
df$col1
#[1] "A" "B" "C"

Upvotes: 1

Anoushiravan R
Anoushiravan R

Reputation: 21908

I think this will work:

library(dplyr)
library(stringr)

df %>%
  mutate(col1 = str_remove(col1, "\\d+(_)"))

  col1
1    A
2    B
3    C

Upvotes: 4

Related Questions