Error in binding rows with variables with date format in R

33 views Asked by At

I have several data frames from several imported excel sheets that have a similar structure and I want to bind them together in one big data frame.

Each data frames from each excel sheet look like this:

structure(list(ID = c(58, 348, 29, 426, 426, 426, 426, 62, 107, 
150, 53, 321, 503, 492, 553, 176, 431, 238, 56, 437, 451, 88, 
447, 199, 402, 542, 592, 475, 457, 324, 30, 306, 379, 251, 54, 
370, 119, 494, 81, 115, 224), Prénom = c(NA, "X", "Y", "Z", "Z", "Z", "Z", "H", "A", "B", "C", "D", "R", "F", "I", "J", "K", "L", "B", "U", "K", "G", "S", "S", "A", "J", "P", "H","D", "X", "W", "L", "F", "K", "K", "I", "M", "J", "L", "I", "F"), caller_id = c("XY", "XY", "XY", "XY", "XY", NA, NA, "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY","XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY", "XY"), Maternité = c("YHN", "YHN","OPS", "OPS", "OPS", NA, NA, "OPS", "OPS", "YHN", 
"YHN", "YHN", "YHN", "YHN", "YHN", "OPS", "OPS", "OPS", NA, NA, "OPS", "OPS", "YHN",  NA, "OPS", "OPS", "YHN", "YHN", "YHN", "YHN", "YHN", "YHN", "OPS", 
"OPS", "OPS", NA, NA, "OPS", "OPS", "YHN",NA, "OPI", "OPI", "KB"), `Date of the survey` = structure(c(1663891200, 1667952000, 1663632000, 1668988800, 1668988800, NA, NA, 1664150400, 
1664841600, 1665360000, 1663804800, 1667779200, 1669852800, 1669766400, 
1670457600, 1665446400, 1668988800, 1666224000, 1663891200, 1669075200, 
1669248000, 1664409600, 1669161600, 1665964800, 1668556800, 1670371200, 
1670976000, 1669593600, 1669248000, 1667779200, 1663632000, 1667433600, 
1668470400, 1666569600, 1663891200, 1668384000, 1664928000, 1669766400, 
1664323200, 1664841600, 1666137600), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Date_dernier_appel = structure(c(1681430400, 1681430400, 
1681430400, 1682035200, NA, 1683849600, 1684108800, 1681430400, 
1681430400, 1681430400, 1681171200, 1681430400, 1681344000, 1681344000, 
1681344000, 1681171200, 1681344000, 1681344000, 1681344000, 1681344000, 
1681344000, 1681344000, 1681344000, NA, 1679443200, 1679616000, 
1678320000, 1679961600, 1679961600, 1679961600, 1679961600, 1681344000, 
1681171200, 1681171200, 1681171200, 1681171200, 1681171200, 1677542400, 
1679616000, 1679702400, 1679788800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"),)

When I try to combine all the data frame I get this error

Error in `bind_rows()`:
! Can't combine `sheet1$Date_dernier_appel` <datetime<UTC>> and `sheet2$Date_dernier_appel` <character>.
Backtrace:
 1. dplyr::bind_rows(all_sheets, .id = "sheet_name")

Here is my code to import the sheets and combine them. As you can see, I tried to convert all columns before binding rows but it doesn't seem to work:

# Import sheets from the Data folder
excel_files <- list.files("../T2/Data", pattern = "\\.xlsx", full.names = TRUE)

# Create a list to store all sheets
all_sheets <- list()

# Convert all dates within each sheet into the same format
for (file in excel_files) {
  sheet_name <- tools::file_path_sans_ext(basename(file))
  sheet <- read_excel(file)
  
  # Apply transformation to columns ending with "/2023"
  sheet <- mutate(sheet, across(ends_with("/2023"), ~ as.Date(.)))
  
  all_sheets[[sheet_name]] <- sheet
}

# Combine all sheets into one big dataset
admin_support <- bind_rows(all_sheets, .id = "sheet_name")

And obviously all dates within the Date_dernier_appel column end with "/2023" I would be so grateful for your help! Thanks

0

There are 0 answers