use index number to name file in a loop in r

You can use a loop and paste the index number as the file name. Here's an example:

main.r
# Set the path where the files will be saved
path <- "~/folder/"

# Create a loop to save files
for(i in 1:10){
  # use paste function to create file name with index number
  filename <- paste0(path, "file_", i, ".csv")
  # create data frame to write to file
  data <- data.frame(x = 1:i, y = i:1)
  # Write file
  write.csv(data, filename, row.names = FALSE)
}
362 chars
13 lines

This will create 10 csv files named "file_1.csv", "file_2.csv", ..., "file_10.csv" in the "folder" directory under your home directory (tilde symbol indicates the home directory).

related categories

gistlibby LogSnag