Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R how to change data types in list.file to prevent bind_rows error

I have multiple (~40) excel files with identical column names and want to bring them all into 1 df and bind_rows but getting error due to data type mismatch. How/when do I convert all to character so that I can bind rows into 1 df?

library(readxl)
library(tidyverse)


file.list <- list.files(pattern='*.xlsx', recursive = TRUE)

df.list <- lapply(file.list, read_excel,"Additions") 

df_all <- bind_rows(df.list, .id = "id")

When I run this I get error

Error in bind_rows():
! Can’t combine 1$ID and 2$ID .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is happening because some ID cols contain characters and some numeric. How do I make all as.character to enable bind_rows?

>Solution :

Sample data a listed data frames:

[[1]]
# A tibble: 10 × 2
   ID    value
   <chr> <int>
 1 1        89
 2 2        30
 3 3        69
 4 4         2
 5 5        52
 6 6        83
 7 7        33
 8 8        67
 9 9         8
10 10       52

[[2]]
# A tibble: 10 × 2
      ID value
   <int> <int>
 1     1    74
 2     2     7
 3     3     2
 4     4    94
 5     5    24
 6     6    32
 7     7    55
 8     8    47
 9     9    49
10    10    42

Convert all ID column to character and then row bind

df_list %>%  
  map_dfr(~ .x %>% 
            mutate(ID = as.character(ID)))

# A tibble: 20 × 2
   ID    value
   <chr> <int>
 1 1        89
 2 2        30
 3 3        69
 4 4         2
 5 5        52
 6 6        83
 7 7        33
 8 8        67
 9 9         8
10 10       52
11 1        74
12 2         7
13 3         2
14 4        94
15 5        24
16 6        32
17 7        55
18 8        47
19 9        49
20 10       42
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading