read in .csv files into a list using a character vector for filenames

35 views Asked by At

I'm reading in a list of .csv files but they're coming in with a single column when in actuality the .csv files have many columns. How can I specify the delimiter is "," when using an lapply function?

files: the character value vector containing filepath names

# produces a list of dfs with only one column
data <- lapply(files, read.delim)

# Next I tried a readr application but that gives me the error
df <- readr::read_delim(files,delim = ",")

Error: Files must all have 57 columns:                                                                                              
    * File 2 has 59 columns

Is there a way to read in files to a list with different numbers of columns?

1

There are 1 answers

0
Alessio On

Another solution similar to the previous one (without using for cycle) is:

setwd ("/yourpath/") 
file_list <- list.files()
dataset <- do.call("rbind",lapply(file_list,
                                  FUN=function(files){read.csv2(files, header=TRUE)}))