# [R] Loop through a simulation

Thu May 16 20:54:05 CEST 2013

```Thanks AK. It works as expected.

Atem.
________________________________________
From: arun [smartpink111 at yahoo.com]
Sent: Thursday, May 16, 2013 11:16 AM
To: Zilefac, Elvis
Cc: R help
Subject: Re: Loop through a simulation

Hi,
May be this is what you meant.

library(plyr)
res1<- ddply(dat1,.(V2,V3,V4),summarize,V5=round(mean(V5),2))
#    V2 V3 V4   V5
#1 1961  1  1 0.52
#2 1961  1  2 0.00
#3 1961  1  3 0.00
#4 1961  1  4 0.00
#5 1961  1  5 0.12
#6 1961  1  6 0.12
tail(res1)
#        V2 V3 V4   V5
#16431 2005 12 26 0.37
#16432 2005 12 27 0.06
#16433 2005 12 28 0.05
#16434 2005 12 29 0.34
#16435 2005 12 30 0.84
#16436 2005 12 31 1.14

#or
res<- aggregate(V5~V2+V3+V4,data=dat1,FUN=mean)
res2<- res[order(res\$V2,res\$V3,res\$V4),]
row.names(res2)<- 1:nrow(res2)
res2\$V5<- round(res2\$V5,2)
identical(res1,res2)
#[1] TRUE
A.K.

----- Original Message -----
From: "Zilefac, Elvis" <aez849 at mail.usask.ca>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Thursday, May 16, 2013 12:05 PM
Subject: RE: Loop through a simulation

Hi AK,

The output looks  a bit different from what I am expecting. The output format should be the same as simulation 1
V1   V2 V3 V4   V5
1  1 1961  1  1 0.51
2  1 1961  1  2 0.00
3  1 1961  1  3 0.00
4  1 1961  1  4 0.00
5  1 1961  1  5 0.00
6  1 1961  1  6 0.00
tail(data)
V1   V2 V3 V4   V5
21 2005 12 26 0.00
21 2005 12 27 0.59
21 2005 12 28 0.00
21 2005 12 29 0.56
21 2005 12 30 3.70
21 2005 12 31 0.00

The output file should resemble the above.
You can arrive at the above output by sorting the first rows of sim 1, sim 2,...sim 21 and then take their mean.
Similarly, take the second row in sim 1, sim 2, ..., sim 21 and take their mean.

This should give you the output above.
Thank so much.
____________
From: arun [smartpink111 at yahoo.com]
Sent: Thursday, May 16, 2013 1:16 AM
To: Zilefac, Elvis
Cc: R help
Subject: Re: Loop through a simulation

HI,
Try this:
dim(dat1)
#[1] 345156      5

res<- aggregate(V5~V2+V3+V4,data=dat1,FUN=mean)
dim(res)
#[1] 16436     4
#    V2 V3 V4         V5
#1 1961  1  1 0.51761905
#2 1962  1  1 0.04666667
#3 1963  1  1 1.14761905
#4 1964  1  1 0.05190476
#5 1965  1  1 0.51428571
#6 1966  1  1 0.50952381
tail(res)
#        V2 V3 V4        V5
#16431 2000 12 31 0.6428571
#16432 2001 12 31 0.1600000
#16433 2002 12 31 0.3314286
#16434 2003 12 31 0.2271429
#16435 2004 12 31 3.7423810
#16436 2005 12 31 1.1371429

A.K.

----- Original Message -----
From: "Zilefac, Elvis" <aez849 at mail.usask.ca>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Thursday, May 16, 2013 2:25 AM
Subject: RE: Loop through a simulation

Dear AK,

Here is data from 21 simulations or so.
Thanks so much.
________________________________________
From: arun [smartpink111 at yahoo.com]
Sent: Thursday, May 16, 2013 12:04 AM
To: Zilefac, Elvis
Subject: Re: Loop through a simulation

Dear Zilefac,

________________________________
From: "Zilefac, Elvis" <aez849 at mail.usask.ca>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Sent: Thursday, May 16, 2013 1:05 AM
Subject: Loop through a simulation

Dear AK,

[simulation, year, month, day, rainfall_values]

I have daily data from 1961 to 2005 . I have made 63 stochastic simulations each from 1961 to 2005.

Now, I would like to obtain the values for each rainfall_value, which is the mean from 63 such values.

For example, rainfall_value of row one should be the mean(simulation 1 row 1, simulation 2 row 1,...simulation  63 row 1) etc. In the end, you will have daily data from 1961 to 200, same length as simulation 1 ,for example.

Thanks so much for this great help.
Atem.

```