nachocab - 2 months ago 13

R Question

Let's say I have these two vectors:

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

y <- c(3,7)

How can I split

`x`

`y`

`c(1,2) | c(4,6,7)`

.

I guess one option would be to do a double loop and return the smallest element in

`y`

`x`

`c(3,3,3,7,7)`

`j <- 1`

sapply(x, function(i){

if (i <= y[j]) {

y[j]

} else {

if (j < length(y)){

j <- j + 1

}

y[j]

}

})

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

Answer

Here is a base R method using `split`

and `findInterval`

:

```
split(x, findInterval(x, y, rightmost.closed=TRUE))
$`0`
[1] 1 2
$`1`
[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.