Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r: pair of values sequence

Tags:

r

seq

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"
...
like image 817
JuanPablo Avatar asked Feb 11 '23 11:02

JuanPablo


2 Answers

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"
like image 91
Colonel Beauvel Avatar answered Feb 13 '23 03:02

Colonel Beauvel


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
like image 37
akrun Avatar answered Feb 13 '23 03:02

akrun