r - How to identify the longest range of consecutive years in a list together with both the start and end date? -
say have list of year integers follows:
olap = c(1992, 1993, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2011, 2012, 2013, 2014);
what least complicated , r-like way of identifying longest range of consecutive years both start date , end date? expect obtain: length: 10, start year: 1997, end year: 2006.
i have been searching little around web including site , people seem recommend using rle() in case. approach solve problem follows:
olap_diff_rle = rle(diff(olap)); max_diff_run = max(olap_diff_rle$lengths[olap_diff_rle$values==1]); idx = cumsum(olap_diff_rle$lengths)[olap_diff_rle$lengths==max_diff_run] + 1; max_olap_end_year = olap[idx]; max_olap_start_year = olap_end_year - max_diff_run; max_olap = max_diff_run + 1;
but appears horribly non-elegant. there must less complicated way of doing this!? want use base r though, no package. have read 1 might use which(diff()!= 1)
identify breaks , continue there?
i approach diff
, rle
this
with(rle(diff(olap)), { dur <- max(lengths[values==1]) end <- sum(lengths[1:which(values==1 & lengths==dur)])+1 list(duration=dur+1, start=olap[end-dur], end=olap[end]) }) # $duration # [1] 10 # # $start # [1] 1997 # # $end # [1] 2006
Comments
Post a Comment