nachocab nachocab - 4 months ago 25
R Question

How to split a vector into unequal chunks in R based on a condition?

Let's say I have these two vectors:

x <- c(1,2,4,6,7)
y <- c(3,7)

How can I split
into the elements that are smaller than each element of
? For example:
c(1,2) | c(4,6,7)

I guess one option would be to do a double loop and return the smallest element in
that is smaller than the current one in
. I could then split using this vector.

j <- 1
sapply(x, function(i){
if (i <= y[j]) {
} else {
if (j < length(y)){
j <- j + 1

I feel like there's a cleverer way to do this, but I can't figure it out.

lmo lmo

Here is a base R method using split and findInterval:

split(x, findInterval(x, y, rightmost.closed=TRUE))

[1] 1 2

[1] 4 6 7

The findInterval function returns a vector that categorizes the variable values in x along your criteria in y. The split function separates the vector as desired and returns a named list.