rreadxlas.date

Excel spreadsheet date column headings imported as character format


My excel spreadsheet always imports to R with column headings like "44983" rather than the d/m/y format that I want. I want to fix this in R rather than in excel. My dataframe is called "backlog" and has 95 columns, the first one is 'Backlog 62d+' and the rest of the columns are these dates like "44983" which R has registered as characters.

I've tried the following code, but my dataframe still looks the same - the column headings haven't changed to date d/m/y format.

backlog <- read_excel("./Weekly/Weekly_Cancer_62d_PTL_Tracker.xlsx", 
                                                sheet = "Provider Trends", range = "B24:CR44" %>% setNames(., c('Backlog 62d+', format(as.Date(as.numeric(names(.)[-1]), origin = '1899-12-30'), '%m/%d/%Y'))))

Solution

  • Based on your comment, I believe this is what you are looking for, with a few transformations :

    vector_column_number <- colnames(backlog)[-1] # Names of your columns without the first one
    list_columns_numeric <- lapply(vector_column_number , as.numeric) # Transformation of the list of character to a list of numeric (needed for as.Date)
    list_columns_date <- lapply(list_columns_numeric , as.Date, origin = "1899-12-30") # Transformation of the numerical value to the date value 
    vector_columns_character <- lapply(list_columns_date , as.character) |> unlist() # Tranformation back to a vector of character
    colnames(backlog) <- c(colnames(backlog)[1],vector_columns_character ) # Change the columns' names, keeping the first one and using the vector created before