Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date sequence in R spanning B.C.E. to A.D

Tags:

date

r

I would like to generate a sequence of dates from 10,000 B.C.E. to the present. This is easy for 0 C.E. (or A.D.):

ADtoNow <- seq.Date(from = as.Date("0/1/1"), to = Sys.Date(), by = "day")

But I am stumped as to how to generate dates before 0 AD. Obviously, I could do years before present but it would be nice to be able to graph something as BCE and AD.

like image 509
madroan Avatar asked Feb 19 '13 20:02

madroan


1 Answers

Quite some time has gone by since this question was asked. With that time came a new R package, gregorian which can handle BCE time values in the as_gregorian method.

Here's an example of piecewise constructing a list of dates that range from -10000 BCE to the current year.

library(lubridate)
library(gregorian)

# Container for the dates
dates <- c()
starting_year <- year(now())
# Add the CE dates to the list
for (year in starting_year:0){
  date <- sprintf("%s-%s-%s", year, "1", "1")
  dates <- c(dates, gregorian::as_gregorian(date))
}

starting_year <- "-10000"
# Add the BCE dates to the list
for (year in starting_year:0){
start_date <- gregorian::as_gregorian("-10000-1-1")
date <- sprintf("%s-%s-%s", year, "1", "1")
dates <- c(dates, gregorian::as_gregorian(date))
}

How you use the list is up to you, just know that the relevant properties of the date objects are year and bce. For example, you can loop over list of dates, parse the year, and determine if it's BCE or not.

> gregorian_date <- gregorian::as_gregorian("-10000-1-1")
> gregorian_date$bce
[1] TRUE
> gregorian_date$year
[1] 10001

Notes on 0AD

The gregorian package assumes that when you mean Year 0, you're really talking about year 1 (shown below). I personally think an exception should be thrown, but that's the mapping users needs to keep in mind.

> gregorian::as_gregorian("0-1-1")
[1] "Monday January 1, 1 CE"

This is also the case with BCE

> gregorian::as_gregorian("-0-1-1")
[1] "Saturday January 1, 1 BCE"
like image 91
Thomas Avatar answered Oct 23 '22 03:10

Thomas