1

I want to convert Birth.Date column into a format as.Date

 $ Birth.Date           : num  38525 39425 39679 39675 39740
Nicolas123
  • 131
  • 1
  • 8
  • 1
    Study `help("as.Date")`, in particular the examples at the bottom. – Roland Jun 03 '21 at 06:17
  • Where did those values come from? If they came from an Excel file it's probably better to fix the loading code so the fields are treated as dates from the start. For example, [readxl](https://cran.r-project.org/web/packages/readxl/readxl.pdf) will read the date values as Posixct as [this answer shows](https://stackoverflow.com/questions/41663890/how-to-read-dates-from-a-excel-files-in-r) – Panagiotis Kanavos Jun 03 '21 at 06:38

2 Answers2

2

This looks like an Excel date.
If this is the case, use as.Date with origin = "1899-12-30":

as.Date(c(38525,39425, 39679, 39675, 39740), origin = "1899-12-30")

#"2005-06-22" "2007-12-09" "2008-08-19" "2008-08-15" "2008-10-19"
Waldi
  • 39,242
  • 6
  • 30
  • 78
1

We could use excel_numeric_to_date from janitor package

library(tibble)
library(janitor)

# vector with your values
date_vector <- c(38525, 39425, 39679, 39675, 39740, 38525, 39425, 39679, 39675, 39740)

# transform to date
excel_numeric_to_date(as.numeric(as.character(date_vector)), date_system = "modern")

Output:

 [1] "2005-06-22" "2007-12-09" "2008-08-19" "2008-08-15" "2008-10-19" "2005-06-22" "2007-12-09" "2008-08-19" "2008-08-15"
[10] "2008-10-19"
TarJae
  • 72,363
  • 6
  • 19
  • 66