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
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With