Search code examples
rexcelreadrreadxl

R changing Excel Date columns Datatype to Logical during read_excel() if Date column is empty in excel


So I have 2 excels VIN1.xlsx and VIN2.xlsx that I need to compare.

VIN1 excel has a dale column OUTGATE_DT which is populated for atleast 1 rows.

VIN2 excel has a date column OUTGATE_DT which is completely null for all rows.

when I import VIN1.xlsx excel using read_excel, it creates the object, and when I check the OUTGATE_DT column, it says its datatype to be as POSIXct[1:4] (which I assume is correct for Date Column. )

But when I import VIN2.xlsx excel using read_excel, it creates the object, and when I check the OUTGATE_DT column, it says its datatype to be logical[1:4] (it is doing this because this column is entirely empty).

and that is why my compare_df(vin1,vin2) functions failing stating -

Error in rbindlist(l, use.names, fill, idcol) : Class attribute on column 80 of item 2 does not match with column 80 of item 1.

I am completely new with R, your help would be highly appreciated. TIA

Please check the screenshot for reference.

enter image description here


Solution

  • Or, if you want to keep the column types in your original df, you can do something like this:

    library(dplyr)
    library(readxl)
    
    VIN2 <- read_excel(VIN2.xlsx) %>%
     mutate(OUTGATE_DT = as.Date(OUTGATE_DT))
    

    then you shouldn't have a problem using rbind or bind_rows from dplyr.