Hi @grethcool and welcome.
To build on what others have said, the dataframe columns must have the same class if you want to combine them. In other words, the id
column must always be character- or integer-class.
I suspect you want id
to be character class, but it does seem odd that the id
might be in the form XYZ123
(character) in one file, but 123456
(integer) in another. It would be a good idea to check that there isn't a problem with the id
column in some of your RDS files.
Below is a reproducible example that you could copy and paste into R on your computer to give you pointers on how to solve your particular problem.
First, we create a couple of fake dataframes and save them to a temporary location. Then we read them in with map_dfr()
like you did, but we make sure this time to convert the id
column to character-class so that they can all be combined.
Let me know if any of this is unclear.
# Attach packages that we'll use
library(dplyr, warn.conflicts = FALSE)
library(readr)
library(tibble)
library(purrr)
# Make two dataframes with 'id' columns of different classes
x <- tibble(id = LETTERS[1:3], value = rnorm(3))
y <- tibble(id = 1:3, value = rnorm(3))
# In dataframe 'x', the id column is character-class
x
#> # A tibble: 3 × 2
#> id value
#> <chr> <dbl>
#> 1 A 0.582
#> 2 B 1.62
#> 3 C 2.38
# In dataframe 'y', the id column is integer-class
y
#> # A tibble: 3 × 2
#> id value
#> <int> <dbl>
#> 1 1 1.07
#> 2 2 -0.391
#> 3 3 -0.355
# You are actually combining more dataframes than this, but I've simplified
# down to just two dataframes for this example.
# You loaded your dataframes as RDS files from your computer, so let's mimic
# that here. We can save our new dataframes as RDS files to a temporary folder
# on our computer (which will be deleted when you close your R session).
temp_folder <- tempdir()
write_rds(x, file.path(temp_folder, "x.rds"))
write_rds(y, file.path(temp_folder, "y.rds"))
# Now let's get the filepaths to the two RDS files we created
rds_files <- list.files(temp_folder, pattern = "*.rds", full.names = TRUE)
# Below is the approach you used, which fails because the 'id' columns in the
# 'x' and 'y' dataframes are different classes (character for 'x' and integer
# for 'y'). (The '..1' and '..2' in the error messages are just the names that
# {readr} gives the dataframes if you don't supply any.)
map_dfr(rds_files, read_rds)
#> Error: Can't combine `..1$id` <character> and `..2$id` <integer>.
# So, we need to convert the 'id' column in all our dataframes to character
# class. There's a few ways to do this. One way is to use mutate() after
# read_rds() in the function that we pass to map_dfr().
# So, In the code below, map_dfr() will read each RDS file and convert the
# 'id' column to character. Once it's read all the files, it will combine them
# all. This time, the 'id' columns will both be character class and so they can
# be combined.
map_dfr(
.x = rds_files,
.f = ~read_rds(.x) %>% mutate(id = as.character(id))
)
#> # A tibble: 6 × 2
#> id value
#> <chr> <dbl>
#> 1 A 0.582
#> 2 B 1.62
#> 3 C 2.38
#> 4 1 1.07
#> 5 2 -0.391
#> 6 3 -0.355
# Great, the 'id' columns from the dataframes in our two RDS were converted
# successfully to character class, allowing them to be combined.
# Notice that we passed to the '.f' argument of map_dfr() a function, which
# is indicated by starting it with a '~' (tilde). We pass '.x' to read_rds()
# because the '.x' argument of map_dfr() is the vector of filepaths to our
# RDS files.
Created on 2021-10-26 by the reprex package (v2.0.1)