Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: extract substring with capital letters from string

I have a dataframe with strings in a column. How could I extract only the substrings that are in capital letters and add them to another column?

This is an example:

    fecha          incident
1   2020-12-01     Check GENERATOR
2   2020-12-01     Check BLADE
3   2020-12-02     Problem in GENERATOR
4   2020-12-01     Check YAW
5   2020-12-02     Alarm in SAFETY SYSTEM

And I would like to create another column as follows:

    fecha          incident                  system
1   2020-12-01     Check GENERATOR           GENERATOR
2   2020-12-01     Check BLADE               BLADE
3   2020-12-02     Problem in GENERATOR      GENERATOR
4   2020-12-01     Check YAW                 YAW
5   2020-12-02     Alarm in SAFETY SYSTEM    SAFETY SYSTEM

I have tried with str_sub or str_extract_all using a regex but I believe I'm doing thigs wrong.

like image 737
MustardRecord Avatar asked Nov 03 '25 03:11

MustardRecord


1 Answers

You can use str_extract if you want to work in a dataframe and tie it into a tidyverse workflow.

The regex asks either for capital letters or space and there need to be two or more consecutive ones (so it does not find capitalized words). str_trim removes the white-space that can get picked up if the capitalized word is not at the end of the string.

Note that this code snipped will only extract the first capitalized words connected via a space. If there are capitalized words in different parts of the string, only the first one will be returned.

library(tidyverse)
x <- c("CAPITAL and not Capital", "one more CAP word", "MULTIPLE CAPITAL words", "CAP words NOT connected")
cap <- str_trim(str_extract(x, "([:upper:]|[:space:]){2,}"))
cap
#> [1] "CAPITAL"          "CAP"              "MULTIPLE CAPITAL" "CAP"

Created on 2021-01-08 by the reprex package (v0.3.0)

like image 54
Mario Niepel Avatar answered Nov 06 '25 01:11

Mario Niepel



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!