Reputation: 3
I am trying to import only 357 specific csv files from a folder with over 2,000 large files. I have a separate list called "names" with just the 357 files I want to analyze:
names$list_id = paste(list_id, 'csv', sep = '.')
names <- as.list(names)
The names now look like this: 987.123.456.csv, 987.123.457.csv....
I know how to import all the files at once and combine them into a single file with the code:
filelist = list.files(pattern = "*.csv")
datalist = lapply(filelist, function(x)read.table(x, h=T))
D = do.call("rbind", datalist)
But I don't want to import all 2,000+ giant files. Is it possible to read just the select files from my list of names that I want into R using a loop?
I am hoping to have just a single file with all the data from just the 357 files I have already filtered and selected. Thank you for your time.
Upvotes: 0
Views: 846
Reputation: 194
Try if this work.
I'll assume you're in the folder where all the files are and that you only want to import the files 1.csv, 10.csv, 20.csv and 40.csv
library(dplyr)
selected_files_to_import <- paste(c(1,10, 20, 40), ".csv", sep = "")
list_of_data_frame <- lapply(selected_files_to_import, function(x){
read.csv(x, stringsAsFactors = F)
})
aggregated_df <- bind_rows(list_of_data_frame)
Another solution could be
library(purrr)
selected_files_to_import <- paste(c(1,10, 20, 40), ".csv", sep = "")
aggregated_df <- map_df(selected_files_to_import,
~ read.csv(., stringsAsFactors = F))
selected_files_to_import is the vector with the files names. You could replace this part whith the list you already have.
Upvotes: 2