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

R 如何在模拟中保存或读取文件,r,loops,simulation,R,Loops,Simulation,我正在尝试运行一个模拟。我需要保存某些文件,有时还需要从模拟内部读取这些文件。我有三个样本量条件,我根据样本量给出名称 例如,文件名为“binAll.100.dne”,这表示样本大小为100 您对如何根据不同的模拟条件保存或读取此排序文件有何想法?这是我的外壳代码。我试图将对象“binAll”保存为“binAll.100.dne”,N是我的样本大小,这里是100 start.time = proc.time() Ns = c(100, 400, 900) # sample sizes Iter

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

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

您对如何根据不同的模拟条件保存或读取此排序文件有何想法?这是我的外壳代码。我试图将对象“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
start.time=proc.time()
Ns=c(100400900)#样本量
迭代次数=300#迭代次数/数据集
用于(N英寸,N英寸){
#将结果存储在空向量中

all.results据我所知,您试图将文件名基于样本大小。您可以使用paste()方法连接文件名和样本大小。请参见下面的示例。如果这不是您想要的,请更新您的问题

N <- 100 # Set sample size

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

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

N要解决的问题到底是什么?“如何根据……条件保存或读取文件”非常模糊……请编辑您的问题。THX!这似乎是一个快速解决方案。谢谢!