问题
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.
回答1:
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')
来源:https://stackoverflow.com/questions/27223366/check-capital-words-in-text-and-extract-it