Saving multiple files to a specified directory

Hi ,
I need to save multiple (just have been created) files to a specified directory:

dat2 <- data.frame(ID = c(1:10))
d_ply(dat2, "ID", function(x)
     write.table(x, file = paste(x$ID[1], "txt", sep = "."), sep = "\t", row.names = FALSE)) %>%

This file.path() does not work here, I don't know where should I place file = "some path" command ?

Thank you in advance for your help.

write.table file = takes your path

I am not sure if I understand your reply Nir correctly.
I know that without specifying a path write.table() will save all these files in my currect working directory,
but I want to save them into another disk and in another directory.
I have come up with this solution:

dat2 = c(1:10)

mypath <- "Q:/TXT2/"

for (i in 1:length(dat2)) {
    file = paste0(mypath, paste(dat2[i], "txt", sep = ".")))

which works very well, but I was wondering if there is any tidyverse alternative ?

its best to use file.path rather than a second paste to glue mypath to the rest , as its more cross platform compatible.
You can optionally use tidyverse purrr to do the iteration

dat2 <- c(1:10)

mypath <- "Q:/TXT2/"

  ~ write.table(dat2[.],
    file = file.path(
      paste0(dat2[.], ".txt")

Thank you very much indeed Nir,
that was very helpful,
best regards,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.