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

Time:12-17

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 .

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

CodePudding user response:

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
  • Related