1

I have a numeric vector with a sequence of years, which might be continuous or can have multiple breaks in between:

years <- c(1990:1994, 1996, 1997, 1999:2002)
years
# 1990 1991 1992 1993 1994 1996 1997 1999 2000 2001 2002

How would I transform this vector to a single character string that describes the ranges of years covered by the vector? Continuous years should be grouped together with a dash, while gaps can be separated with commas. Desired output:

# "1990-1994, 1996-1997, 1999-2002"
sebpardo
  • 707
  • 11
  • 17
  • 2
    Reading from the duplicate, `R.utils::seqToHumanReadable(years, 1)` would be exactly what you're looking for. – josliber Mar 11 '20 at 16:05
  • Thanks @josliber, I did look around if this had been asked before but could not find any answers. I figured it must have been! – sebpardo Mar 11 '20 at 16:09
  • 1
    Glad the duplicate solved your issue! In case it helps, the google query I used to find the duplicate was `find sequences of consecutive numbers r site:stackoverflow.com`. – josliber Mar 11 '20 at 16:34

0 Answers0