I want to import and bind in one data frame a set of .csv files.
@richie-cotton provided an answer that fixed your path issue, but I want to address the highlighted part of your question.
Richie Cotton's code:
library(assertive.files)
files_to_import <- list.files(some_path, full.names = TRUE)
assert_all_are_existing_files(files_to_import)
data <- lapply(files_to_import, read.csv)
Will end in data
, a list of data.frames. You want these to be one data.frame
. Take a look at this answer to a similar question: https://stackoverflow.com/a/18489830/5333994
My suggestion would be to end with using rbindlist
on the resulting list of data.frame
.
library(data.table)
data <- rbindlist(data)
In fact, if you're going to use data.table
you can replace read.csv
with fread
which will likely speed things up.
library(data.table)
library(assertive.files)
files_to_import <- list.files(some_path, full.names = TRUE)
assert_all_are_existing_files(files_to_import)
data <- rbindlist(lapply(files_to_import, fread))
If you want to end in a data.frame
rather than data.table
simply use setDF
.
setDF(data)
If you want to avoid data.table
all together, running do.call
is probably your best option.
data <- do.call("rbind", data)
Note from the answer I linked above that this is likely to be a much slower operation than using rbindlist
, depending on the number of files you have.