jay_phate
jay_phate

Reputation: 439

check capital words in text and extract it

I want to extract all capital words from the text. Lets say my data is like-->

Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY)

So output should be like -->

> output

[1] "I JAY" "I AM NOT HAPPY" "YOU ARE IRRITATING" "" ""

Please help me for this.

Upvotes: 4

Views: 2276

Answers (1)

akrun
akrun

Reputation: 887213

Another option is

library(stringr)
sapply(str_extract_all(Text, '\\b[A-Z]+\\b'), paste, collapse=' ')
# [1] "I JAY"              "I AM NOT HAPPY"     "YOU ARE IRRITATING"
#[4] ""                   ""    

Or

 gsub("[a-z][A-Za-z]+|[A-Za-z][a-z]+", '', Text)
 #[1] "I  JAY"             "I AM NOT HAPPY"     "YOU ARE IRRITATING"
 #[4] " "                  ""                  

data

 Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY')

Upvotes: 4

Related Questions