Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Date Conversion Issue in R [duplicate]

Tags:

r

excel

Running into an issue when drawing in Excel data from R and converting to a date within R. I have a "time_period" column that is pulled from Excel in that Excel date format with 5 digit numbers (e.g. 41640).

> head(all$time_period)
[1] "41640" "41671" "41699" "41730" "41760" "41791"

These numbers are originally in chr format so I change them to numeric type with:

all[,3] <- lapply(all[,3], function(x) as.numeric(as.character(x)))

Once that is complete, I run the below to format the date:

all$time_period <-format(as.Date(all$time_period, "1899-12-30"), "%Y-%m-%d")

However, once this action is completed the time_period column is all the same date (presumably the first date in the column).

> head(all$time_period)
[1] "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" "2014-01-01" 

Any suggestions? Thanks in advance.

like image 592
Franchise Avatar asked Sep 06 '25 04:09

Franchise


2 Answers

set the origin argument in as.Date()

These numbers refer to distances away from an origin, which depends on the machine the excel file was created on.

  • Windows: as.Date(my_date, origin = "1899-12-30")
  • Mac: as.Date(my_date, origin = "1904-01-01")

For example:

x <- c("41640","41671","41699","41730","41760","41791")
x <- as.numeric(x)
format(as.Date(x, "1899-12-30"), "%Y-%m-%d")

Returns:

[1] "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01"
like image 162
JayCe Avatar answered Sep 08 '25 16:09

JayCe


I believe this one line solves your problem, you don't need to format it, as de default of as.Date function is "%Y-%m-%d".

 time_period = c("41640", "41671", "41699", "41730", "41760", "41791")

 as.Date(as.numeric(time_period), origin =  "1899-12-30").
like image 43
Giovana Stein Avatar answered Sep 08 '25 18:09

Giovana Stein