Save .RData in a different directory

1.3k Views Asked by At

I load my files (.RData) from a particular folder, and i created a subfolder to save some samples and subsets. So, i want to save these elements in the subfolder, and they don't have the same name structure because i have multiple datasets (for example it cannot be sub1, sub2 etc, i have to write try1, full_sample, sub_2021 and so on).

I tried the following :

subsets_samples <- file.path <-("/Volumes/WD_BLACK/Merge/SAMPLES_SUBSETS")
fname <- file.path(subsets_samples, ".RData")
save(mydata, file=fname)

But obviously there is a problem with the saving part. My goal is to have something like :

save(mydata, file = "newname")

With the .RData format from fname that is put automatically.

I saw some answers with loops and so on but i don't really understand the process i'm sorry.

Thanks !

1

There are 1 best solutions below

8
On

The problem with file.path is that it will place a separator (e.g., /¸) between each of the elements. So you would have to use paste0 in addition for the actual file name:

# If I understand you correctly, you want the iteration, like try1, full_sample, sub_2021 and so on in your file name. define them somewhere in your loop/script
iteration <- "full_sample"
fname <- file.path("Volumes", "WD_BLACK", "Merge", "SAMPLES_SUBSETS", paste0(iteration, ".Rds"))

Additionally, I would suggest to use saveRDS instead of save, since it is the appropriate function if you want to save just one object.

saveRDS(mydata, file = fname)