tzema
tzema

Reputation: 461

Sequence of dates for non-gregorian calendar in R

I would like to get a sequence of dates for a year with 12 months- 30 days each (so no Gregorian calendar), and I am wondering if there is some workaround in seq.Date or seq.IDate (data.table). Thank you!

Upvotes: 1

Views: 110

Answers (3)

Patrick
Patrick

Reputation: 32244

You are probably working with some data set that conforms to the CF Metadata Conventions (such as most climate projection data and lots of other environmental data sets). Of the 9 supported calendars 360_day represents years as 12 months of 30 days. That is obviously not POSIXt compliant so you can't reliably use the builtin date functions, nor a package like lubridate. There is now, however, a package CFtime which supports all 9 CF calendars transparently.

You can create a time series for the year 2022 like so:

ts <- CFtime("days since 2022-01-01", "360_day", 0:359)

or for the period 2020-12-20 to 2021-1-10:

ts <- CFtime("days since 2020-12-20", "360_day", 0:20)

And as character strings:

CFtimestamp(ts)

The package can also create factors that are calendar-aware for further processing.

Upvotes: 0

Darren Tsai
Darren Tsai

Reputation: 35584

You could use ISOdate() to create date-times from numeric representations.

as.Date(ISOdate(2022, rep(1:12, each = 30), 1:30))

Upvotes: 1

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

  • We can try this
as.Date(paste0( "2022/",rep(1:12 , each = 30) ,"/", rep(1:30 , times = 12)))

Upvotes: 1

Related Questions