Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequence expansion question

Tags:

r

sequences

I have a sequence of 'endpoints', e.g.:

c(7,10,5,11,15)     

that I want to expand to a sequence of 'elapsed time' between the endpoints, e.g.

c(7,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,1,2,3,4,5,6,7,8,9,10,11,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

Whats the most efficient way to do this in R? I'm imagining some creative use of the embed function, but I can't quite get there without using a ugly for loop.

Here's the naive way to do this:

expandSequence <- function(x) {
    out <- x[1]
    for (y in (x[-1])) {
        out <- c(out,seq(1,y))
    }
    return(out)
}

expandSequence(c(7,10,5,11,15))
like image 419
Zach Avatar asked Aug 03 '11 22:08

Zach


3 Answers

There is a base function to do this, called, wait for it, sequence:

sequence(c(7,10,5,11,15))

 [1]  1  2  3  4  5  6  7  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  1  2  3
[26]  4  5  6  7  8  9 10 11  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

In your case it seems your first endpoint is in fact not part of the sequence, so it becomes:

c(7, sequence(c(10,5,11,15)))
 [1]  7  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  1  2  3  4  5  6  7  8  9
[26] 10 11  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
like image 105
Andrie Avatar answered Nov 13 '22 02:11

Andrie


How about this:

> unlist(sapply(x,seq))
 [1]  1  2  3  4  5  6  7  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  1  2
[25]  3  4  5  6  7  8  9 10 11  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

With the first element added on at the end:

c( x[1], unlist( sapply( x[seq(2,length(x))], seq ) ) )

And a slightly more readable version:

library(taRifx)
c( x[1], unlist( sapply( shift(x,wrap=FALSE), seq ) ) )
like image 21
Ari B. Friedman Avatar answered Nov 13 '22 03:11

Ari B. Friedman


A combination of lapply() and seq_len() is useful here:

expandSequence <- function(x) {
    out <- lapply(x[-1], seq_len)
    do.call(c, c(x[1], out))
}

Which gives for

pts <- c(7,10,5,11,15)

> expandSequence(pts)
 [1]  7  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  1  2  3  4
[21]  5  6  7  8  9 10 11  1  2  3  4  5  6  7  8  9 10 11 12 13
[41] 14 15

(An alternative is:

expandSequence <- function(x) {
    out <- lapply(x[-1], seq_len)
    unlist(c(x[1], out), use.names = FALSE)
}

)

like image 3
Gavin Simpson Avatar answered Nov 13 '22 02:11

Gavin Simpson