XUN ZHANG
XUN ZHANG

Reputation: 93

how to export every output in a loop in R?

library(SMFI5)

for(i in 1:10)

{
 simulateloop<-sim.vasicek(0.001987455,0.001614103,0.000186756,-0.001,30,1)
 print(simulateloop)
}

simulateloop

The sim.vasicek() function will creat both data output and a graph output, I only want to export every data output by row or by column, so i can use colMeans() or rowMeans() to calculate the average value of these 10 simulations, but I can only get one data output rather than 10 different data output. Could anyone tell me how to export all the 10 different data output by row?

Upvotes: 0

Views: 119

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388797

You can use replicate which will return you 10 columns on which you can use colMeans

library(SMFI5)
simulateloop <- replicate(10, sim.vasicek(0.001987455,0.001614103,0.000186756,-0.001,30,1))

To use for loop you can store the result in a list

simulateloop <- vector('list', length = 10)

for(i in 1:10){
  simulateloop[[i]]<- sim.vasicek(0.001987455,0.001614103,0.000186756,-0.001,30,1)
}

and then use sapply to take mean

sapply(simulateloop, mean)

Upvotes: 3

Related Questions