Search code examples
rreadr

Define one column only when reading in a list of csv files?


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))


Solution

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