简体   繁体   中英

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. 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:

"ab", "ac", "ac", "ab"

Thanks for any thoughts.

If you are not wedded to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM