2
votes

I am trying to import multiple CSVs from a folder at once, but the CSVs do not have column names. The following code works, but the first row is converted into column names:

dat <- list.files(pattern="*.csv") %>% lapply(read.csv)

When I try to use the code below:

dat <- list.files(pattern="*.csv") %>% lapply(read.csv(header = FALSE))

I get the following error message:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : argument "file" is missing, with no default

Any idea how I can avoid this?

2
try dat <- list.files(pattern="*.csv") %>% lapply(function(x) read.csv(x,header = F))? - Vitali Avagyan

2 Answers

4
votes

The issue comes from incorrect specifying of additional parameters to FUN.

? lapply:

lapply(X, FUN, ...)

...       optional arguments to FUN.

You need to make a tiny change to your code to get it to work:

dat <- list.files(pattern="*.csv") %>% lapply(read.csv, header=FALSE)
1
votes

If you're in the tidyverse you might want

list.files(pattern=".csv") %>%
   purrr::map(readr::read_csv, col_names=FALSE)

(watch out for differences in default behaviour between read.csv and readr::read_csv)