Reputation: 439
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
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] " " ""
Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY')
Upvotes: 4