0

I want to import multiple CSV simultaneously in R and I don’t want to merge them. I want them to be quickly accessible to do ICC after. I tried the code below. When I double click on each file in the global environment in Rstudio and error appear in the console Error: unexpected symbol in "View(X.csv". I tried other methods, but they didn’t give the result I wanted, or they didn’t solve my problem.

The 1243 CSV files are in the same folder (i.e., Tableau des features).
The name's files are 16.CSV, 17.CSV …,1257.CSV, 1258.CSV
All the files have the same structure which consists of a data.frame of 5 columns and 83 rows.

library(data.table)
setwd("/Users/T/Desktop/Rstudio/Tableau des features/")
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
  • Maybe this would help you https://datascienceplus.com/how-to-import-multiple-csv-files-simultaneously-in-r-and-create-a-data-frame/ – Sachin Yadav Jul 28 '20 at 14:40
  • I already tried this method, but it open all the files in a list. The initial presentation of the data is lost in the process... – Tchat Cusson Jul 28 '20 at 14:56
  • It should be fairly trivial but there are a bunch of missing information (please read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) as it's a great resource on how to write Qs on SO). Secondly, do all the .csv files have the same structure (columns)? – anddt Jul 28 '20 at 16:25
  • Thanks for the link ! I think your answer could work with me project ! – Tchat Cusson Jul 28 '20 at 17:59
  • @TchatCusson If the answer suits your use case, consider closing your question by flagging it as approved. – anddt Jul 28 '20 at 18:09
  • Oh, I didn't know this function ! I'm really new with R and SO. – Tchat Cusson Jul 28 '20 at 18:12

1 Answers1

0

It's not quite clear how you'd like to return the formatted data. Assuming you're trying to return another data frame you can store all the files in a list and merge them together with bind_rows from {dplyr} package.

library(dplyr)

files <- list.files(path = "data/", pattern="*.csv")
data <- lapply(files, function(x) {
    read.csv(paste0("data/", x))
})

data <- bind_rows(data)
head(data)
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> 2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> 3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> 4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> 5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> 6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Created on 2020-07-28 by the reprex package (v0.3.0)

anddt
  • 1,589
  • 1
  • 9
  • 26