Warm tip: This article is reproduced from serverfault.com, please click
csv r

Repeating same task with for loop using filename pattern in csv in r

发布于 2020-11-28 06:12:54

I am repeating some tasks in R. I first read the csv file, do some repeating work, and write the csv file based on the date on the filename. I want to use for loop by using the filename pattern especially using day value (e.g. 17 in below example) say from 1 to 31. Could anyone help me how to code for loop here? Thanks in advance.

text <- read_csv("D://2017-10-17.csv")

... Some work here ...

write_csv(text , "2017-10-17_csv_backup.csv", na = "") 
Questioner
B.Lee
Viewed
0
stefan 2020-11-28 14:28:13

You could do

for (i in 1:31) {
  text <- read_csv(paste0("D://2017-10-", i, ".csv"))

  ... Some work here ...

  write_csv(text , paste0("2017-10-", i, "_csv_backup.csv"), na = "")
}