[R] Looping

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Mon Feb 19 05:19:53 CET 2024


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 using 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 using 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.



More information about the R-help mailing list