Home > Back-end >  R function for trimming beginning of a string
R function for trimming beginning of a string

Time:02-10

Here is a sample of the dataset I am working with.

df <- data.frame( precinct = c("01-001", "01-002", "Adv 1 01-001", "Adv 1 01-002"), total = c(100, 102, 4, 6))

I need to remove the specific string "Adv 1 " while leaving everything else intact. I considered stringr functions like str_replace() and str_remove() but but I understand it only works on single letters. Is there a function or procedure for performing the trimming?

CodePudding user response:

You can use gsub() to substitute the text to empty.

UPDATE 2: as pointed out by @r2evans:
If you have multiple patterns of "Adv 1 " in the string and you only wish to replace the one that is at the beginning, use "^Adv 1 " in the codes instead of "Adv 1 " (e.g. df %>% mutate(precinct = gsub("^Adv 1 ", "", precinct))).

^ is a regular expression (regex) that specify the beginning of a string.

Thanks @r2evans for the suggestion!

Tidyverse

This will not replace your df unless you assign the result back to it.

library(tidyverse)

df %>% mutate(precinct = gsub("Adv 1 ", "", precinct))

UPDATE: in the comment, @Onyambu suggested me to include the following codes for the tidyverse solution. Thanks @Onyambu!

df %>% mutate(precinct = str_remove(precinct, 'Adv 1 '))

And

df %>% mutate(precinct = str_replace(precinct, 'Adv 1 ', ''))

Base R

This will immediately replace your df.

df[, 1] <- gsub("Adv 1 ", "", df[, 1])

CodePudding user response:

Another base R solution :

df <- data.frame( precinct = c("01-001", "01-002", "Adv 1 01-001", "Adv 1 01-002"), total = c(100, 102, 4, 6))
df[,1] <- trimws(df[,1], whitespace =  "Adv 1")
  •  Tags:  
  • r
  • Related