Removing parts of string variable based on placement of -

Hello Everyone,

thank you for your help on this small problem.

I have the following data frame

'''

Name <- c("Practice - Sept. 1, 2021 - Firstname Lastname", "Practice - Sept. 30, 2021 - John Doe")
Value <- c(4,6)
DF <- data.frame(Name, Value)

'''

I would like to remove the term "Practice" and the Date from the string variable DF$Name so that

'''

DF$Name <- c("Firstname Lastname", "John Doe")

'''

I am new to R and have not fully grasped the basics of expression language (which I assume I will need to successfully solve this issue)

Thank you!

The regular expression I used means:
look behind for a hyphen and a space and then match one or more characters that are not hyphens until the end of the string.
(?<=- ) means look behind for a hyphen and space
[^-]+ means one or more characters that are not a hyphen
$ designates the end of the string.

library(dplyr, warn.conflicts = FALSE)
library(stringr)
Name <- c("Practice - Sept. 1, 2021 - Firstname Lastname", "Practice - Sept. 30, 2021 - John Doe")
Value <- c(4,6)
DF <- data.frame(Name, Value)
DF <- DF %>% mutate(Name = str_extract(Name, "(?<=- )[^-]+$"))
DF
#>                 Name Value
#> 1 Firstname Lastname     4
#> 2           John Doe     6

Created on 2021-10-04 by the reprex package (v2.0.1)

2 Likes

Thank you for the information and also the helpful explanation!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.