Reading Multiple CSV Files from a Folder into a Single Dataframe in R

Reading Multiple CSV files as data frames in R

There are so many ways!!

setwd("C:/your_path_here")
fnames <- list.files()
csv <- lapply(fnames, read.csv)
result <- do.call(rbind, csv)


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, skip = 1, header = FALSE))


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


filedir <- setwd("C:/your_path_here")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, header = FALSE))


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


#
temp <- setwd("C:/your_path_here")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


# Here is another options to convert the .csv files into one data.frame. Using R base functions.
# This is order of magnitude slower than the options below.

files <- setwd("C:/your_path_here")
# Get the files names
files = list.files(pattern="*.csv")
# First apply read.csv, then rbind
myfiles = do.call(rbind, lapply(files, function(x) read.csv(x, stringsAsFactors = FALSE)))

library(readr)
library(dplyr)
tbl = lapply(files, read_csv) %>% bind_rows()


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


# LIST OF FILE PATHS

library(readr)
library(stringr)

List_of_file_paths <- list.files(path ="C:/your_path_here/", pattern = ".csv", all.files = TRUE, full.names = TRUE)


******** ******** ******** ******** ******** ******** ******** ******** ******** ********


# LIST OF FILES IN FOLDER
xlist<-list.files(pattern = "*.csv")

for(i in xlist) {
x <- read.csv((i))
assign(i, x)
}


******** ******** ******** ******** ******** ******** ******** ******** ******** ********

How to import multiple .csv files from folder into R and select columns?

You may try this approach -

#column names
cols <- c('col1', 'col5', 'col6', ...)
#Or column numbers
#cols <- c(1, 5, 6, ...)

library(dplyr)
library(purrr)

all_files <- list.files('/csv/folder', pattern = '\\.csv$', full.names = TRUE)
result <- map_df(all_files,
~.x %>% readr::read_csv() %>% select(cols), .id = 'filenum')

result

In result, I have also created an additional column called filenum which will indicate the file number from where the row is originating.

Read multiple csv files (and skip 2 columns in each csv file) into one dataframe in R?

Using the data.table package functions fread() and rbindlist() will provide the result you're after faster than any of the other base or tidyverse alternatives.

library(data.table)

## Create a list of the files
FileList <- list.files(pattern = ".csv")

## Pre-allocate a list to store all of the results of reading
## so that we aren't re-copying the list for each iteration
DTList <- vector(mode = "list", length = length(FileList))

## Read in all the files, excluding the first two columns
for(i %in% seq_along(DTList)) {
DTList[[i]] <- data.table::fread(FileList[[i]], drop = c(1,2))
}

## Combine the results into a single data.table
DT <- data.table::rbindlist(DTList)

## Optionally, convert the data.table to a data.frame to match requested result
## Though I would recommend looking into using data.table instead!
data.table::setDF(DT)

How to load multiple csv files into seperate objects(dataframes) in R based on filename?

Solution for anyone curious...

files <- list.files(pattern = ".*csv")

for(file in 1:length(files)) {
file_name <- paste(c("file00",file), collapse = " ")
file_name <- gsub(" ", "", file_name, fixed = TRUE)
ex_file_name <- paste(c("exfile00",file), collapse = " ")
ex_file_name <- gsub(" ", "", ex_file_name, fixed = TRUE)

file_object <- read.csv(file = paste(file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
exfile_object <- read.csv(file = paste(ex_file_name, ".csv", sep=""),fileEncoding="UTF-8-BOM")
}

Essentially build the filename within the loop, then passs it to the readcsv function on each iteration.

Read multiple csv into one and add a new column based on the file names

You can use sapply to read all the files in a list and with rbindlist combine them into one dataframe with a new column filename which has name of the file in every row.

library(data.table)
result <- rbindlist(sapply(files, fread,simplify = FALSE), idcol = 'filename')


Related Topics



Leave a reply



Submit