r - difference between a list of dates -


i'm stuck problem of calculating differences in dates in list. develop question in 2 points:

1.

i have vector of dates. find difference in days between each couple of dates following each others. example, between date1 , date2, date2 , 3, 3 , 4... since strptime function seems work 2 dates, how can compute difference in days between these couples of dates ?

[1] "2011-10-06" "2011-09-28" "2011-09-20" "2011-08-29" "2011-09-09" "2011-08-16" [7] "2011-08-05" "2011-07-28" "2011-07-18" "2011-07-06" "2011-06-27" "2011-06-17" [13] "2011-06-03" "2011-05-22" "2011-05-02" "2011-05-12" "2011-04-21" "2011-04-11" [19] "2011-04-01" "2011-03-23 

2.

these dates contained in list this:

 $ 2008: chr [1:17] "2008-05-22" "2008-05-30" "2008-06-09" "2008-06-18" ...  $ 2003: chr [1:23] "2003-04-24" "2003-05-15" "2003-05-23" "2003-06-05" ...  $ 2005: chr [1:22] "2005-04-22" "2005-05-07" "2005-05-14" "2005-05-22" ...  $ 2006: chr [1:24] "2006-04-14" "2006-04-19" "2006-05-03" "2006-05-11" ...  $ 2007: chr [1:15] "2007-06-15" "2007-04-27" "2007-05-11" "2007-05-22" ...  $ 2004: chr [1:20] "2004-05-18" "2004-06-04" "2004-06-09" "2004-06-18" ...  $ 2009: chr [1:19] "2009-05-05" "2009-05-19" "2009-05-29" "2009-06-10" ...  $ 2010: chr [1:18] "2010-09-28" "2010-09-20" "2010-09-10" "2010-09-01" ...  $ 2011: chr [1:20] "2011-10-06" "2011-09-28" "2011-09-20" "2011-08-29" ... 

how apply answer previous question each of thes objects?

i hope question clear enough!

thanks!

no need lapply, unlist , use diff has method date class objects...

dates <- as.list( c( "2011-10-06","2011-09-28","2011-09-20","2011-08-29","2011-09-09","2011-08-16","2011-08-05","2011-07-28","2011-07-18","2011-07-06","2011-06-27","2011-06-17","2011-06-03","2011-05-22","2011-05-02","2011-05-12","2011-04-21","2011-04-11","2011-04-01","2011-03-23" ) )  diff( as.date( unlist(dates) ) ) #time differences in days # [1]  -8  -8 -22  11 -24 -11  -8 -10 -12  -9 -10 -14 -12 -20  10 -21 -10 -10  -9 

Comments