Error Combining Data Frames in R with `dplyr::bind_rows()

Hello everyone,

I'm encountering an issue while running a section of my R code that involves loading and processing point of interest (POI) data. The part of the code causing the error is:

# Load data --------------------------------------------------------------------
if(SURVEY_NAME == "LAGOS_POINTS"){
  n_poi_df <- file.path(data_dir, SURVEY_NAME, 
                        "FinalData", "Individual Datasets", "osm", "poi") %>%
    list.files(full.names = T,
               pattern = "*.Rds") %>%
    str_subset("_n_poi_1000m") %>%
    map_df(readRDS)
  
  names(n_poi_df) <- names(n_poi_df) %>%
    str_replace_all("1000m", "5000m")
  
} else{
  n_poi_df <- file.path(data_dir, SURVEY_NAME, 
                        "FinalData", "Individual Datasets", "osm", "poi") %>%
    list.files(full.names = T,
               pattern = "*.Rds") %>%
    str_subset("_n_poi_5000m") %>%
    map_df(readRDS)
}

dist_poi_df <- file.path(data_dir, SURVEY_NAME, 
                         "FinalData", "Individual Datasets", "osm", "poi") %>%
  list.files(full.names = T,
             pattern = "*.Rds") %>%
  str_subset("_dist_poi") %>%
  map_df(readRDS)

However, I'm getting the following error:

Error in `dplyr::bind_rows()`:
! Can't combine `..1$osm_mindistmeters_poi_theme_park` <data.frame> and `..2$osm_mindistmeters_poi_theme_park` <double>.
Run `rlang::last_trace()` to see where the error occurred.

It seems like the dplyr::bind_rows() function is having trouble combining data frames where the osm_mindistmeters_poi_theme_park column is of different types (data.frame in one case and double in another).

Has anyone encountered a similar issue or have any suggestions on how to resolve this? Any help would be greatly appreciated!

Thank you!

you first have to have an understanding if it is justifiable that osm_mindistmeters_poi_theme_park can sometimes be a data.frame and sometimes be a double.
if it can, you have to figure the more generic type and coerce the one to the other , likely enframing double values into a data.frame . if it cant that opens a wide door ,depending on what responsibility if any you have for the input data, because it may need remediation at source... understanding what went wrong so it can be best fixed.

Hi!,

You are attempting to bind two data frames together where the columns have incompatible types: e.g "double" and "character".

Check the columns and ensured they have consistent data types in both datasets you are attempting to combine. For example, if there are date column in the datasets make sure they are of the same data type. If in the first dataset the date column is of "character" type and is of "double" type in the second dataset then it will throw up an error.

This might not necessary be just the date column. So you have to look at all the columns and ensure data type consistency.

Note that the error is not in column names but rather in column types.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.