简体   繁体   中英

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.

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))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM