如何在模拟中保存或读取文件

How to save or read files inside of a simulation

我正在尝试 运行 模拟。有些文件我需要保存,有时需要从模拟内部读取。我有三个样本量条件,我根据样本量给出名称。

例如,文件名为“binAll.100.dne”,样本大小为 100。

您对如何根据不同的模拟条件保存或读取此类文件有什么想法吗?这是我的 shellcode。我正在尝试将对象“binAll”保存为“binAll.100.dne”和N 是我的样本量,这里是 100。

start.time = proc.time()

Ns = c(100, 400, 900) # sample sizes
Iterations = 300 #number of iterations/datasets

for (N in Ns){

  #store the results in an empty vector
  all.results <- c() 

  for (iter in 1:Iterations){
  # ALL FUNCTIONS GO HERE
WriteNetworks(binAll,"binAll.100.dne") # how to save this seperately for each sample size      

  } #close dataset loop

  # save the results outside of the dataset loop
  write.table(all.results, file="simulation_results.csv", sep=",", append=T,col.names=F,row.names=F,quote=F) 

  } #close the sample size loop

end.time = proc.time()
total.time = end.time - start.time

感谢您抽出宝贵时间。 干杯。

据我了解,您正试图根据样本大小来命名文件。您可以通过使用 paste() 方法连接文件名和样本大小来完成此操作。请参见下面的示例。如果这不是您想要的,请更新您的问题。

N <- 100 # Set sample size

# Create filename
fileName <- paste("binAll", N, "dne", sep=".") 
print(fileName) 

# Example write function
write.table(yourData, file=fileName)