Robin Kohrs
Robin Kohrs

Reputation: 697

Create a list of vectors from a vector where n consecutive values are not 0 in R

So I have this vector:

a = sample(0:3, size=30, replace = T)
 [1] 0 1 3 3 0 1 1 1 3 3 2 1 1 3 0 2 1 1 2 0 1 1 3 2 2 3 0 1 3 2

What I want to have is a list of vectors with all the elements that are separated by n 0s. So in this case, with n = 0 (there can't be any 0 between the consecutive values), this would give:

res = c([1,3,3], [1,1,1,3,3,2,1,1,3], [2,1,1,2]....)

However, I would like to control the n-parameter flexible to that if I would set it for example to 2, that something like this:

b = c(1,2,0,3,0,0,4)

would still result in a result like this

res = c([1,2,3],[4])

I tried a lot of approaches with while loops in for-loops while trying to count the number of 0s. But I just could not achieve it.

Update

I tried to post the question in a more real-world setting here: Flexibly calculate column based on consecutive counts in another column in R

Thank you all for the help. I just don't seem to manage put your help into practice with my limited knowledge..

Upvotes: 0

Views: 196

Answers (3)

Patrick Bormann
Patrick Bormann

Reputation: 749

I also wanted to paste a somehow useful solution with the function SplitAt from DescTools:

SplitAt(a, which(a==0)) %>% lapply(., function(x) x[which(x != 0)])

where a is your intial vector. It gives you a list where every entry contains the pair of numbers between zeros:

1st solution

If you than add another SplitAt with empty chars, you can create sublist after sublist and split it in as many sublists as you want: e.g.:

n <- 4
SplitAt(a, which(a==0)) %>% lapply(., function(x) x[which(x != 0)]) %>% SplitAt(., n)

gives you:

enter image description here

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101363

Here is a base R option using rle + split for general cases, i.e., values in b is not limited to 0 to 3.

with(
  rle(with(rle(b == 0), rep(values & lengths == n, lengths))),
  Map(
    function(x) x[x != 0],
    unname(split(b, cut(seq_along(b), c(0, cumsum(lengths))))[!values])
  )
)

which gives (assuming n=2)

[[1]]
[1] 1 2 3

[[2]]
[1] 4

If you have values within ragne 0 to 9, you can try the code below

lapply(
  unlist(strsplit(paste0(b, collapse = ""), strrep(0, n))),
  function(x) {
    as.numeric(
      unlist(strsplit(gsub("0", "", x), ""))
    )
  }
)

which also gives

[[1]]
[1] 1 2 3

[[2]]
[1] 4

Upvotes: 1

Phil
Phil

Reputation: 8107

set.seed(1)
a <- sample(0:3, size=30, replace = T)
a
[1] 0 3 2 0 1 0 2 2 1 1 2 2 0 0 0 1 1 1 1 2 0 2 0 0 0 0 1 0 0 1

a2 <- paste(a, collapse = "") # Turns into a character vector, making it easier to handle patterns.
a3 <- unlist(strsplit(a2, "0")) # Change to whatever pattern you want, like "00".
a3 <- a3[a3 != ""] # Remove empty elements
a3 <- as.numeric(a3) # Turn back to numeric
a3
[1]     32      1 221122  11112      2      1      1

Upvotes: 0

Related Questions