I know theres a lot of code that can import and combine multiple CSV files, but I need to also change the delimiter.
The code I'm using is
df <- list.files() %>%
lapply(read_csv) %>%
bind_rows
Is there a way to alter that code to specify the delimiter as |?
Thanks
CodePudding user response:
You can do it more easier way than additionally using bind_rows
library(readr)
paths <- list.files()
read_delim(paths, id = "path", delim = "|")
this will automatically read all the files in the paths obtained by list.files
and bind them
CodePudding user response:
use pmap
function if you need different separators in your csv files
require(purrr)
setwd('d:/tmp/SO_csvfiles/')
flist <- list.files()
seps <- c(',',';','|')
data.list <- pmap(list(flist, seq_along(flist)), function(fname, .index) {
read.csv(fname, sep=seps[.index])
})
bind_rows(data.list)
CodePudding user response:
The lapply()
function accepts a ...
(dot-dot-dot) argument and passes that on to FUN (dot-dot-dot explained), so
df <- list.files() %>%
lapply(read_delim, delim = "|") %>%
bind_rows
is how you set the delimiter to "|" for the read_delim()
function.
Another potential alternative is to identify the files you're interested in and import them using vroom()
which will guess the delimiter and also provide an "ID" column indicating which rows came from which csv (although obviously you don't need to use this if you don't want):
library(fs)
library(vroom)
file_list <- fs::dir_ls(path = "/your_path/to_your_files", glob = "*.txt")
data <- vroom::vroom(file_list, id = "filename")