How to import multiple .csv in multiple data.frame?

Hi, guys!

I have been trying to import 4 .csv files at once.

Each .csv will be a data.frame.

My code:

files <- list.files(path = "data-raw/csgo_stat/", pattern = "*.csv") |> stringr::str_remove(".csv")

multiple_csv <- sapply(paste0("data-raw/csgo_stat/",files,".csv"), read.csv)

for(i in 1:length(multiple_csv)){
  
  assign(paste0(files[[i]]), multiple_csv |> purrr::pluck(i))

}

This worked, but I'm pretty sure that there is a easy way to do it.

Thanks a lot!

If you want the data to be in separate data frames, you could try this. I like datatable because its faster on large files.

  library(dplyr)
  library(data.table)
  data_path <- "data-raw/csgo_stat/"
  files_df <- paste0(data_path, list.files(path = data_path, pattern = "*.csv")) %>% lapply(FUN=function(f) { 
 fread(f) })
1 Like

Cool!

Thanks for the sugestion!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.