Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract text after first upper case or space

How can I extract all text after first space in a column where data is something like this

structure(list(value = c("1.1.a Blue sea", "1.2.a Red ball")), row.names = c(NA, -2L), class =c("tbl_df", "tbl", "data.frame"))

so I get a new column with just

Blue sea
Red ball

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the following code to select all text after the first white space:

sub("^\\S+\\s+", '', df$value)

Output:

[1] "Blue sea" "Red ball"

You can just use this to create it as a new column:

library(dplyr)
df %>%
  mutate(new_value = sub("^\\S+\\s+", '', value))

Output:

# A tibble: 2 × 2
  value          new_value
  <chr>          <chr>    
1 1.1.a Blue sea Blue sea 
2 1.2.a Red ball Red ball 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading