1

I'm reading in a list of files and combining into a single df with:

fi <- list.files(path = 'data', pattern = '*.csv', full.names = T) %>%  
  map_df(~read_csv(paste0(.)))

But this is hitting an error:

Error: Can't combine App ID <character> and App ID <double>. .

Within the directory in question there are many files to read in and combine. I'd like to know if it's possible to define the column type for the problem field, App ID while reading in like this? E.g. map_df(~read_csv(paste0(.), cols(App ID = CHR))

user14328853
  • 414
  • 2
  • 10

1 Answers1

1

We can use

library(dplyr)
library(purrr)
 out <-  list.files(path = 'data', pattern = '*\\.csv',
    full.names = TRUE) %>%             
       map_df(~read_csv(.x) %>%
            mutate(across(everything(), as.character())) %>%
       type.convert(as.is = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Hi, I had to update the error message above since it was using <> chars I had to escape them. It's the data types among the various csvs that are clashing. Any ideas? `Error: Can't combine `App ID` and `App ID` .` – user14328853 Mar 01 '21 at 22:56
  • @user14328853 please check the updte – akrun Mar 01 '21 at 23:01
  • Hi, thanks for trying here. Appreciate I never provided reproducible data but this doesn't work. It cannot get past the map_df() step since that's the step that throws the error. If I could somehow tell R to force field `App ID` to chr during read_csv that would do it. But reading the ?read_csv documentation I don't see how :/ – user14328853 Mar 01 '21 at 23:03
  • @user14328853 why it doesn't work. It is converting all columns to `character` within the `map` and binding. So, it should work – akrun Mar 01 '21 at 23:04
  • Oh I see, yes I misread your code. Hm, let me try again... – user14328853 Mar 01 '21 at 23:06
  • 1
    Yep, it does work. Never realized or thought to mutate the df within the map_df function itself, thanks! – user14328853 Mar 01 '21 at 23:09