Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make B.C. Date format in R

Tags:

date

r

lubridate

I have string like 1/1/-2150. How to make Date format from that in R

lubridate back:

library(lubridate)
dmy("1/1/-2150")
[1] "2150-01-01"

as.Date("1/1/-2150",format="%d/%m/%Y")
[1] NA

Now 1/1/-2150 have class character.
I need same value but with class Date
Thanks

UPDATE
Something like that, but using lubridate if it possible

minus=as.numeric(dmy("1/1/-2150"))
x<-as.numeric(ymd("0000-1-1"))
dt=as.Date(x*2-minus,origin="1970-01-01")+days(1) 
str(dt)
Date[1:1], format: "-2150-01-01"
like image 260
jyjek Avatar asked Sep 19 '25 11:09

jyjek


2 Answers

Unfortunately there aren't any really large R packages tackling this issue (maybe no one has asked). The gregorian package should however, be able to tackle your BCE needs.

gregorian::as_gregorian("-2150-1-1") [1] "Tuesday January 1, 2151 BCE"

like image 167
Thomas Avatar answered Sep 22 '25 07:09

Thomas


Late to the party but here is a possible solution here. Find how far 2150-01-01 is from 0000-01-01 and then make it a negative number and pass it to as.Date() with the origin '0000-01-01'

y <- as.numeric(as.Date("2150-01-01"))+ as.numeric(as.Date("1970-01-01") - as.Date("0000-01-01"))
y
#785272

x <- as.Date(-y, "0000-01-01")
x
#"-2151-12-31"
like image 42
Mike Avatar answered Sep 22 '25 08:09

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!