2

I'm loading an Excel file which has a column where dates are stored as integers (43000 for example is 2017-09-22). I tried applying the answer to this question this way:

df = df %>%
  mutate(date = as.Date(date, origin = "1899-12-30"))

But I'm having the following error:

Error in mutate_impl(.data, dots) : Evaluation error: character string is not in a
standard unambiguous format. Calls: (...)

I searched for this problem and all results are about incorrectly specifying a string input to as.Date but in this situation the input is an integer.

gsmafra
  • 2,434
  • 18
  • 26

2 Answers2

6

Just found the problem: the dates were somehow loaded as string instead of integers. This solved my problem:

df = df %>%
  mutate(date = as.Date(as.numeric(date), origin = "1899-12-30"))
gsmafra
  • 2,434
  • 18
  • 26
0

Using janitor::excel_numeric_to_date can make your code slightly more concise

library(janitor)
df = df %>%
  mutate(date = excel_numeric_to_date(as.numeric(date)))

Manny Ma
  • 93
  • 1
  • 1
  • 11