【问题标题】:How to save or read files inside of a simulation如何在模拟中保存或读取文件
【发布时间】:2018-11-09 20:53:49
【问题描述】:

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

例如,文件名为“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

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

【问题讨论】:

  • 究竟要解决什么问题? “如何根据......条件保存或读取文件”非常模糊......请编辑您的问题。谢了!

标签: r loops simulation


【解决方案1】:

据我了解,您正在尝试将文件名基于样本大小。您可以通过使用 paste() 方法连接文件名和样本大小来做到这一点。请参见下面的示例。如果这不是您所追求的,请更新您的问题。

N <- 100 # Set sample size

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

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

【讨论】:

  • 这似乎是一个快速的解决方案。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
相关资源
最近更新 更多