我想从文本中提取所有大写单词。可以说我的数据就像 - >
Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY)
因此输出应该像 - &gt;
> output
[1]&#34; I JAY&#34; &#34;我不快乐&#34; &#34;你是兴奋的&#34; &#34;&#34; &#34;&#34;
请帮助我。
答案 0 :(得分:4)
另一种选择是
library(stringr)
sapply(str_extract_all(Text, '\\b[A-Z]+\\b'), paste, collapse=' ')
# [1] "I JAY" "I AM NOT HAPPY" "YOU ARE IRRITATING"
#[4] "" ""
或者
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')