Rafael
Rafael

Reputation: 3196

split vector of strings with partial match

If I have a list of some elements:

x = c('abc', 'bbc', 'cd', 'hj', 'aa', 'zz', 'd9', 'jk')

I'd like to split it every time there's an 'a' to create a nested list:

[1][[1]] 'abc', 'bbc', 'cd', 'hj'
[2][[1]] 'aa', 'zz', 'd9', 'jk'

I tried

split(x, 'a') 

but split doesn't look for partial matches.

Upvotes: 1

Views: 136

Answers (3)

tmfmnk
tmfmnk

Reputation: 40151

Another base R possibility could be:

split(x, cumsum(nchar(sub("a", "", x, fixed = TRUE)) - nchar(x) != 0))

$`1`
[1] "abc" "bbc" "cd"  "hj" 

$`2`
[1] "aa" "zz" "d9" "jk"

Upvotes: 2

ThomasIsCoding
ThomasIsCoding

Reputation: 102349

Another base R solution using split + findInterval (code is not as short as the answer by @akrun)

split(x,findInterval(seq_along(x),grep("a",x)))

such that

> split(x,findInterval(seq_along(x),grep("a",x)))
$`1`
[1] "abc" "bbc" "cd"  "hj" 

$`2`
[1] "aa" "zz" "d9" "jk"

Upvotes: 1

akrun
akrun

Reputation: 887611

We can create a group by matching the substring 'a' with grepl to a logical vector and then convert to numeric by getting the cumulative sum for distinct groups and use that in split

split(x, cumsum(grepl('a', x)))
#$`1`
#[1] "abc" "bbc" "cd"  "hj" 

#$`2`
#[1] "aa" "zz" "d9" "jk"

Upvotes: 1

Related Questions