How to Read All Files in One Directory into R at Once

How to import multiple .csv files at once?

Something like the following should result in each data frame as a separate element in a single list:

temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)

This assumes that you have those CSVs in a single directory--your current working directory--and that all of them have the lower-case extension .csv.

If you then want to combine those data frames into a single data frame, see the solutions in other answers using things like do.call(rbind,...), dplyr::bind_rows() or data.table::rbindlist().

If you really want each data frame in a separate object, even though that's often inadvisable, you could do the following with assign:

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))

Or, without assign, and to demonstrate (1) how the file name can be cleaned up and (2) show how to use list2env, you can try the following:

temp = list.files(pattern="*.csv")
list2env(
lapply(setNames(temp, make.names(gsub("*.csv$", "", temp))),
read.csv), envir = .GlobalEnv)

But again, it's often better to leave them in a single list.

R read all files in a directory

I suspect the problem lies in the path to the files. Most likely because your working directory is one level up from the directory "test". Try:

    list.files(path = "test", pattern = ".csv", full.names = TRUE)

The full.names argument will include the path to the files in it's output.

Read all files in specific folder in R

Set your base directory, and then use it to create a vector of all the files with list.files, e.g.:

base_dir <- 'path/to/my/working/directory'
all_files <- paste0(base_dir, list.files(base_dir, recursive = TRUE))

Then just loop over all_files. By default, list.files has recursive = FALSE, i.e., it will only get the files and directory names of the directory you specify, rather than going into each subfolder. Setting recursive = TRUE will return the full filepath excluding your base directory, which is why we concatenate it with base_dir.

read all file in one data.frame (rlang)

You have two problems here: (1) how to read that file, once; (2) how to read many files that look like that.

files <- list.files("~/StackOverflow/13426927", pattern = "asc$", full.names = TRUE)
files
# [1] "C:\\Users\\r2/StackOverflow/13426927/61704300.R.asc"
# [2] "C:\\Users\\r2/StackOverflow/13426927/61704300_2.R.asc"

read.table(files[1], header = FALSE, skip = 8)
# V1 V2 V3 V4
# 1 87406 2041 6779 20743
# 2 87407 2014 6778 21553
# 3 87408 2041 6780 20743
# 4 87409 2041 6781 20743
# 5 87410 2041 6782 20743
# 6 87411 2014 6778 21553

Now that we know how to read one at a time, now all at once:

alldat <- lapply(files, read.table, header = FALSE, skip = 8)
out <- do.call(rbind.data.frame, alldat)
out
# V1 V2 V3 V4
# 1 87406 2041 6779 20743
# 2 87407 2014 6778 21553
# 3 87408 2041 6780 20743
# 4 87409 2041 6781 20743
# 5 87410 2041 6782 20743
# 6 87411 2014 6778 21553
# 7 87406 2041 6779 20743
# 8 87407 2014 6778 21553
# 9 87408 2041 6780 20743
# 10 87409 2041 6781 20743
# 11 87410 2041 6782 20743
# 12 87411 2014 6778 21553

(Rename as needed.)

How do you read multiple .txt files into R?

Thanks for all the answers!

In the meanwhile, I also hacked a method on my own. Let me know if it is any useful:

library(foreign)

setwd("/path/to/directory")

files <-list.files()

data <- 0

for (f in files) {

tempData = scan( f, what="character")

data <- c(data,tempData)

}


Related Topics



Leave a reply



Submit