Convert Excel numeric to date
Solution 1
You can simply use as.Date
and specify the origin, i.e.
as.Date(date, origin="1899-12-30")
#[1] "2017-08-16" "2017-09-16" "2017-06-17" "2017-07-17" "2017-08-17"
#or format it to your liking,
format(as.Date(date, origin="1899-12-30"), '%b %Y')
#[1] "Aug 2017" "Sep 2017" "Jun 2017" "Jul 2017" "Aug 2017"
This link gives quite a bit of information on this matter.
Solution 2
If you want to convert dates from Excel, you can use as.Date()
with a specific origin
. According to the documentation, "1900-01-0"'
is used as day in Excel on Windows, but "this is complicated by Excel incorrectly treating 1900 as a leap year"
. So "1899-12-30"
should be used for dates post 1901:
date <- c(42963,42994,42903,42933,42964)
This is the result of as.Date()
:
as.Date(date, origin = "1899-12-30")
[1] "2017-08-18" "2017-09-18" "2017-06-19" "2017-07-19" "2017-08-19"
You can then use zoo::as.yearmon()` to get the expected outcome:
zoo::as.yearmon(as.Date(date, origin = "1899-12-30"))
[1] "Aug 2017" "Sep 2017" "Jun 2017" "Jul 2017" "Aug 2017"
Solution 3
Type excel_numeric_to_date
to look at the function's code and you'll see it's a wrapper for the line of code used by the other answers to this question: as.Date(date_num, origin = "1899-12-30")
.
So that's not the issue.
The underlying matter here is confusion about date formatting. You say you expect your first number 42963
to become "Aug 2016"
, and your last number 42964
to become "Aug 2017"
. The latter is just one more than the former, which shows up in the conversion - they should be a day apart, not a year apart as you are expecting:
> excel_numeric_to_date(c(42963, 42964))
[1] "2017-08-16" "2017-08-17" # as expected, they are one day apart
Perhaps the day and year fields are switched upstream in your data at the point where these get mapped to integer dates, and it was hard to tell here because of the values chosen.
Azam Yahya
Updated on June 28, 2022Comments
-
Azam Yahya almost 2 years
I have a vector of numeric excel dates i.e.
date <- c(42963,42994,42903,42933,42964)
The output am I expecting when using
excel_numeric_to_date
function fromjanitor
package andas.yearmon
function fromzoo
packageas.yearmon(excel_numeric_to_date(date)) [1] "Aug 2016" "Sep 2016" "Jun 2017" "Jul 2017" "Aug 2017"
.However, the conversion for the first to elements of the
date
vector are incorrect. The actual result are:as.yearmon(excel_numeric_to_date(date)) [1] "Aug 2017" "Sep 2017" "Jun 2017" "Jul 2017" "Aug 2017"
I have tried using different option(
modern
andmac pre-2011
) for thedate_system
argument in theexcel_numeric_to_date
but it does not help eitherThe excel version is 2010