emehex emehex - 1 month ago 9
R Question

Remove everything after last space with stringr

I have data that looks like this:

df <- tribble(
~name, ~value,
"Jake Lake MLP", 10,
"Bay May CE", 5,
"Drake Cake Jr. DSF", 9.1,
"Sam Ram IR QQQZ", 1
)


I want to trim all the names so that they are:

"Jake Lake",
"Bay May",
"Drake Cake Jr.",
"Sam Ram IR"


Basically removing everything after the last space.

I tried:

df %>% mutate(name = str_replace(name, "\\s.*$", ""))


But it's not quite what I want!

Answer

We can use sub

df %>% 
    mutate(name = sub("\\s+[^ ]+$", "", name))

Or the same pattern in str_replace

df %>% 
   mutate(name = str_replace(name, "\\s[^ ]+$", ""))
# A tibble: 4 × 2
#            name value
#           <chr> <dbl>
#1      Jake Lake  10.0
#2        Bay May   5.0
#3 Drake Cake Jr.   9.1
#4     Sam Ram IR   1.0

The pattern indicates a space (\\s) followed by one or more non white space (otherwise it can \\S+) until the end of the string and replace it with blank "". In the OP's code, it was non-specific (.*).