I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, data<-read.csv(?data1.csv?) ? data<-read.csv(?data24.csv?) ? Is there a way to do this in a loop? Thank you. Steven from iPhone [[alternative HTML version deleted]]
Try for (ind in 1:24) { data = read.csv(paste0("data", ind, ".csv")) ... } Peter On Mon, Feb 19, 2024 at 11:33?AM Steven Yen <styen at ntu.edu.tw> wrote:> > I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, > > data<-read.csv(?data1.csv?) > ? > data<-read.csv(?data24.csv?) > ? > > Is there a way to do this in a loop? Thank you. > > Steven from iPhone > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Steven, It depends what you want to do. What you are showing seems to replace the values stored in "data" each time. Many kinds of loops will do that, with one simple way being to store all the filenames in a list and loop on the contents of the list as arguments to read.csv. Since you show filenames as having a number from 1 to 24 in middle, you can make such a vector using paste(). A somewhat related question is if you want to concatenate all the data into one larger data.frame. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Steven Yen Sent: Sunday, February 18, 2024 10:28 PM To: R-help Mailing List <r-help at r-project.org> Subject: [R] Looping I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, data<-read.csv(?data1.csv?) ? data<-read.csv(?data24.csv?) ? Is there a way to do this in a loop? Thank you. Steven from iPhone [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
f <- function (filename) { data<- read.csv(filename) .. } for (filename in paste0("data", 1:24, ".csv")) f(filename) Depending on what exactly you have in your file system, for (filename in system("ls data*.csv", TRUE)) f(filename) might work. On Mon, 19 Feb 2024 at 16:33, Steven Yen <styen at ntu.edu.tw> wrote:> > I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, > > data<-read.csv(?data1.csv?) > ? > data<-read.csv(?data24.csv?) > ? > > Is there a way to do this in a loop? Thank you. > > Steven from iPhone > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
?s 03:27 de 19/02/2024, Steven Yen escreveu:> I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, > > data<-read.csv(?data1.csv?) > ? > data<-read.csv(?data24.csv?) > ? > > Is there a way to do this in a loop? Thank you. > > Steven from iPhone > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Here is a way of reading the files in a *apply loop. The file names are created by getting them from file (list.files) or by a string editing function (sprintf). # file_names_vec <- list.files(pattern = "data\\d+\\.csv") file_names_vec <- sprintf("data%d.csv", 1:24) data_list <- sapply(file_names_vec, read.csv, simplify = FALSE) # access the 1st data.frame data_list[[1L]] # same as above data_list[["data1.csv"]] # same as above data_list$data1.csv Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
In my package HelpersMG, I have included a function to read in one time all the files of a folder and they are stored in a list: read_folder( ? folder = try(file.choose(), silent = TRUE), ? file = NULL, ? wildcard = "*.*", ? read = read.delim, ? ... ) In your case, for example: library("HelpersMG") data_list <- read_folder(folder=".", file=paste0("data", as.character(1:24),".csv"), read=read.csv) data_df <- ? do.call("rbind", data_list) Marc Le 19/02/2024 ? 04:27, Steven Yen a ?crit?:> I need to read csv files repeatedly, named data1.csv, data2.csv,? data24.csv, 24 altogether. That is, > > data<-read.csv(?data1.csv?) > ? > data<-read.csv(?data24.csv?) > ? > > Is there a way to do this in a loop? Thank you. > > Steven from iPhone > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.