简体   繁体   English

Save.RData 在不同的目录中

[英]Save .RData in a different directory

I load my files (.RData) from a particular folder, and i created a subfolder to save some samples and subsets.我从特定文件夹加载我的文件 (.RData),并创建了一个子文件夹来保存一些示例和子集。 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).所以,我想将这些元素保存在子文件夹中,并且它们没有相同的名称结构,因为我有多个数据集(例如它不能是 sub1、sub2 等,我必须编写 try1、full_sample、sub_2021 等等)。

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.使用来自 fname 的 .RData 格式自动放入。

I saw some answers with loops and so on but i don't really understand the process i'm sorry.我看到了一些带有循环等的答案,但我不太了解这个过程,对不起。

Thanks !谢谢 !

The problem with file.path is that it will place a separator (eg, / ¸) between each of the elements. file.path的问题在于它会在每个元素之间放置一个分隔符(例如, / ¸)。 So you would have to use paste0 in addition for the actual file name:因此,您还必须使用paste0作为实际文件名:

# 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而不是save ,因为如果您只想保存一个 object,它是合适的 function。

saveRDS(mydata, file = fname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM