Reputation: 85
Please tell me how can I save my loop results into the table(data.frame) or even .csv file. I can't handle with that issue by myself.
for (i in 1:10000){
x<-pois(1,40)
sum<-round(digits = 2, sum(rlnorm(x, log(10), log(5))))
}
Upvotes: 0
Views: 212
Reputation: 85
yes indeed, I meant rpois function. I solved it using the next formula:
Results=matrix(0,10000,1)
for ( i in 1:10000)
{
x<-rpois(1,40)
Results[i,1]<-round(digits = 2, sum(rlnorm(x,log(20), log(4))))
}
Upvotes: 0
Reputation: 123
If you want to use a for loop, create an empty vector and then iterate through each position.
mySums <- numeric(10000)
for (i in 1:10000){
x <- rpois(1,40)
mySums[i] <- round(digits = 2, sum(rlnorm(x, log(10), log(5))))
}
It is straightforward to then turn this into a dataframe or any other format you require.
Edit: This is assuming you meant to use rpois()
or something similar.
Upvotes: 2
Reputation: 12569
Assuming that you want to call rpois()
(and not pois()
):
mySums <- replicate(10000, round(digits=2, sum(rlnorm(rpois(1,40), log(10), log(5)))))
Upvotes: 1