Frameworker247 Frameworker247 - 10 months ago 37
R Question

Create a vector of multiple series from two vectors

I'm sure there is a super-simple solution to this, I just can't figure out what it is.

I have a long, single-variable dataset in R. I can identify the beginning and end of the multiple relevant ranges throughout, I just need to filter out the rest.

So given a vector x:

> x <- c(2400:2600)

And vectors of start points and end points:

> startpoints <- c(1,11,101)
> endpoints <- c(3,13,103)

I need to return the subset of x that looks like this:

> x[c(1:3, 11:13, 101:103)]
[1] 2400 2401 2402 2410 2411 2412 2500 2501 2502

I've tried this:

> x[startpoints:endpoints]

But R just takes the first value from each vector and returns that series:

[1] 2400 2401 2402

...with a couple of warnings

My startpoints and endpoints vectors should always be equal in length, although they may vary together in length with different datasets.

Answer Source

We can use mapply to get the sequence between corresponding elements of 'startpoints', 'endpoints' and this can subset the 'x' vector.

x[mapply(`:`, startpoints, endpoints)]
#[1] 2400 2401 2402 2410 2411 2412 2500 2501 2502

If the sequence are of different lengths, as @nicola mentioned, mapply returns a list, so we need to unlist it

x[unlist(mapply(`:`, startpoints, endpoints))]