R - avoid new column when merging csv files

Viewed 44

I have a script that merge all csv files in a folder. My problem is that a new column named "...20" is created with empty data. How can I avoid that ? Thanks for helping

My script :

folderfiles <- list.files(path = "//myserver/Depots/",
                          pattern = "\\.csv$",
                          full.names = TRUE)

data_csv <- folderfiles %>% 
  set_names() %>% 
  map_dfr(.f = read_delim,
          delim = ";",
          )

and the message :

console

1 Answers

It's difficult to debug this without access to specific files. However, you can attempt to specify the columns you want to read using the cols_only function. For example, let's assume that you only want to read the mpg column. You can do that in the following manner:

library("fs")
library("readr")
library("tidyverse")

# Generating some sample files
temp_dir_files <- path_temp("cars")
dir_create(temp_dir_files)
for (i in 1:10) {
    write_csv(mtcars, file = path(temp_dir_files, paste0("cars", i, ".csv"))) 
}

# Selected column import
# read_* can handle a vector of paths
read_csv(
    file = dir_ls(temp_dir_files, glob = "*.csv"),
    col_types = cols_only(
        mpg = col_double()
    ),  
    id = "input_file"
)

The cols_only specification passed to read_csv will force the read_csv to skip the remaining columns and only import the column with the matching name.

Related