检查文本中的大写单词并将其解压缩

时间:2014-12-01 07:12:51

标签: r gsub

我想从文本中提取所有大写单词。可以说我的数据就像 - >

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;

请帮助我。

1 个答案:

答案 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')