Tibble Upload in R / Date Format

28 views Asked by At

can someone help me with the following problem? I have an excel sheet with different date formats in different columns. I would like to upload this excel file as a tibble in R Studion exactly the way it is shown in the excel file. As you can see,however, columns "date2", "date5", "date6", "date7"and "date8" are not uploading the same way as the other columns. How can I modify the code I am using (df <- read_excel('raw_data.xlsx')? The object can stay a Character in the outcome as I can then convert it into a date format.

enter image description here

df <- read_excel('raw_data.xlsx')

enter image description here

Thank you for your help!!

I have already tried to set check.names to FALSE but that wont work either

1

There are 1 answers

0
Carl On

Excel stores dates internally as "days since origin". You can convert them per the example below:

(It's a good idea to confirm the origin date for the version of Excel you're using, or you may be able to confirm it if you know what one of the dates should be.)

library(tidyverse)

df <- tribble(
  ~date1, ~date2,
  "2014-05-15", 41774,
  "2014-05-16", 41775
)

df |>
  mutate(date3 = as_date(date2, origin = "1899-12-30"))
#> # A tibble: 2 × 3
#>   date1      date2 date3     
#>   <chr>      <dbl> <date>    
#> 1 2014-05-15 41774 2014-05-15
#> 2 2014-05-16 41775 2014-05-16

Created on 2024-03-20 with reprex v2.1.0