Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select string after second space

Tags:

regex

r

tidyverse

I have a column containning different names, I would like to get all the strings after the second space of characters.

My example.

df <- data.frame(col = c("Adenia macrophylla", "Adinobotrys atropurpureus (Wall.) Dunn", "Ardisia purpurea Reinw. ex Blume"))

My desired outcome like this

                                     col
1                                    
2                           (Wall.) Dunn
3                        Reinw. ex Blume

Any sugesstions for me? The way before I did is to separate them and unite, but I consider whether we have any fancy way or better to do it, since I already have many columns.

Update Just solve it

xx %>% 
  mutate(col = str_pad(col, 20,"right")) %>% 
  mutate(col = str_remove(col, '\\w+\\s\\w+\\s'))

Thanks @Ronak and @U12-Forward for providing me regex

like image 307
Anh Avatar asked Dec 13 '25 10:12

Anh


1 Answers

You may use sub -

sub('\\w+\\s\\w+\\s', '', df$col)
#[1] "(Wall.) Dunn"    "Reinw. ex Blume"
#Also
#sub('.*?\\s.*?\\s', '', df$col)

If you want a tidyverse answer.

library(dplyr)
library(stringr)

df %>% mutate(val = str_remove(col, '\\w+\\s\\w+\\s'))
like image 123
Ronak Shah Avatar answered Dec 16 '25 00:12

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!