Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

Suppose we have a folder containing multiple data.csv files, each containing the same number of variables but each from different times. Is there a way in R to import them all simultaneously rather than having to import them all individually?

My problem is that I have around 2000 data files to import and having to import them individually just by using the code:

read.delim(file="filename", header=TRUE, sep="\t")
is not very efficient.

1 Answer

0 votes
by

To read multiple csv files at once, you can use the read_csv function from the readr package as follows:

To get the filenames If the files are in your working directory:

file_names = list.files(pattern="*.csv")

To read the files:

library(readr)

library(dplyr)

fls = lapply(files_names, read_csv) %>% bind_rows()

You can also use the read.delim function as follows:

file_names = list.files(pattern="*.csv")

myfiles = lapply(file_names, read.delim)

To combine these data frames into a single data frame:

DF = do.call(rbind, lapply(files_names, read.delim))

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...