JuanPablo
JuanPablo

Reputation: 24834

r: pair of values sequence

in R, with seq, I can get a date sequence

seq(as.Date('2014-02-01'), as.Date('2014-8-31'), by='1 month')
[1] "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01"

how I can get a pair of values? a interval of values over the sequence

"2014-02-01" "2014-03-01"
"2014-03-01" "2014-04-01"
...

Upvotes: 4

Views: 605

Answers (2)

Colonel Beauvel
Colonel Beauvel

Reputation: 31181

If you want a list you can do:

s1 = seq(as.Date('2014-02-01'), as.Date('2014-8-31'), by='1 month')

Map(c, head(s1, -1), tail(s1, -1))
#[[1]]
#[1] "2014-02-01" "2014-03-01"

#[[2]]
#[1] "2014-03-01" "2014-04-01"

#[[3]]
#[1] "2014-04-01" "2014-05-01"

#[[4]]
#[1] "2014-05-01" "2014-06-01"

#[[5]]
#[1] "2014-06-01" "2014-07-01"

#[[6]]
#[1] "2014-07-01" "2014-08-01"

Upvotes: 5

akrun
akrun

Reputation: 887951

You can try

 s1 <- seq(as.Date('2014-02-01'), as.Date('2014-8-31'), by='1 month')
 d1 <- data.frame(v1=s1[-length(s1)], v2=s1[-1])
 d1
 #        v1         v2
 #1 2014-02-01 2014-03-01
 #2 2014-03-01 2014-04-01
 #3 2014-04-01 2014-05-01
 #4 2014-05-01 2014-06-01
 #5 2014-06-01 2014-07-01
 #6 2014-07-01 2014-08-01

Upvotes: 3

Related Questions