【发布时间】:2018-10-17 08:35:35
【问题描述】:
我读过一些关于拆分大写和小写的好问题,例如 this 和 this,但我无法让它们与我的数据一起使用。
# here my data
data <- data.frame(text = c("SOME UPPERCASES And some Lower Cases"
,"OTHER UPPER CASES And other words"
, "Some lower cases AND UPPER CASES"
,"ONLY UPPER CASES"
,"Only lower cases, maybe"
,"UPPER lower UPPER!"))
data
text
1 SOME UPPERCASES And some Lower Cases
2 OTHER UPPER CASES And other words
3 Some lower cases AND UPPER CASES
4 ONLY UPPER CASES
5 Only lower cases, maybe
6 UPPER lower UPPER!
想要的结果应该是这样的:
V1 V2
1 SOME UPPERCASES And some Lower Cases
2 OTHER UPPER CASES And other words
3 AND UPPER CASES Some lower cases
4 ONLY UPPER CASES NA
5 NA Only lower cases, maybe
6 UPPER UPPER! lower
因此,将所有仅包含大写字母的单词与其他单词分开。
作为测试,我只尝试了一些方法,但它们都不能很好地工作:
strsplit(x= data$text[1], split="[[:upper:]]") # error
gsub('([[:upper:]])', ' \\1', data$text[1]) # not good results
library(reshape)
transform(data, FOO = colsplit(data$text[1], split = "[[:upper:]]", names = c('a', 'b'))) # neither good results
【问题讨论】:
-
不清楚你的规则是什么。您想省略最后一行中的
!,但保留上一行中的,。您在这里的确切规则是什么? -
非常感谢,有错别字,标点跟上一个字母的大小写一致。