سؤال

I am trying to run a function on a batch of files within a directory, how can I write each output to a file that is named according to its origin? E.g:

files <- dir(pattern="*.csv")

for (i in 1:length(files)){
  dat<-reformat.funct(files[i])
  write.csv(dat, "???.reform.csv")
}

I would like to replace the "???" above with the i'th file name from "files." Suggestions on more efficient ways to do it also welcome (for scripts longer than one line).

Thank you.

هل كانت مفيدة؟

المحلول

given files:

 out.files <- gsub("\\.csv$", ".reform.csv", files)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top