[英]Match and concatenate characters with grepl() in R
I would like to use the grepl() function to determine whether a vector of characters is matched with a pattern and based on that pattern concatenate characters within the vector. 我想使用grepl()函数来确定字符向量是否与模式匹配,并基于该模式连接向量中的字符。 For example: 例如:
vec <- c("a","b","a","c","a","c","a","b")
grepl("[a]", vec)
TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
I would like the all of the values following the TRUE to be binded together until the next TRUE so that the outcome of this would be a vector that looks like: 我希望将TRUE之后的所有值绑定到一起,直到下一个为TRUE,这样它的结果将是一个看起来像这样的向量:
"ab", "ac", "ac", "ab"
Thanks for any thoughts. 谢谢你的任何想法。
If you are not wedded to grepl()
: 如果你不grepl()
:
VEC <- paste(vec, collapse="") # Collapse into single string ...
strsplit(VEC, "(?<=.)(?=a)", perl=TRUE)[[1]] # ... then split it before each 'a'
# [1] "ab" "ac" "ac" "ab"
Use this: 用这个:
groups <- cumsum(grepl("[a]", vec))
# > groups
# [1] 1 1 2 2 3 3 4 4
aggregate(vec, by=list(groups=groups), FUN=function(x)paste(x,collapse=""))
# groups x
# 1 1 ab
# 2 2 ac
# 3 3 ac
# 4 4 ab
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.