[R] for loop help

jim holtman jholtman at gmail.com
Tue Jan 29 23:54:07 CET 2008


Is this what you want?

> numSim <- 15
>
> genx<-double(numSim)
> N <- rep(20, numSim)
> A <- F <- M <- numeric(numSim)
>
> result <- lapply(1:5, function(.x){
+     for (i in 1:numSim) {
+         PN<-(runif(N[i], 0, 1))
+         A[i]<-sum(ifelse(PN>0.2, 1, 0))
+         PF<- runif((A[i]*0.5), 0, 1)
+         F[i]<-sum(ifelse(PF>0.2, 1, 0))
+         PM<- runif((A[i]*0.5), 0, 1)
+         M[i]<-sum(ifelse(PM>0.2, 1, 0))
+         genx[i]<- (min(F[i], M[i])*12)
+     }
+
+     data <- data.frame(N, A, F, M, genx)
+     N <<- genx   # assign at the top level
+     data   # return the data frame
+ })
> result
[[1]]
    N  A F M genx
1  20 16 6 7   72
2  20 12 6 4   48
3  20 16 5 8   60
4  20 18 7 6   72
5  20 14 6 7   72
6  20 18 8 8   96
7  20 15 3 7   36
8  20 13 5 6   60
9  20 15 7 5   60
10 20 15 6 7   72
11 20 16 6 5   60
12 20 13 5 6   60
13 20 15 6 6   72
14 20 15 6 7   72
15 20 20 8 8   96

[[2]]
    N  A  F  M genx
1  72 60 21 25  252
2  48 39 17 15  180
3  60 50 18 22  216
4  72 55 20 18  216
5  72 57 22 25  264
6  96 72 29 25  300
7  36 28 12  8   96
8  60 51 18 22  216
9  60 49 19 20  228
10 72 56 24 21  252
11 60 47 19 17  204
12 60 44 18 20  216
13 72 59 21 26  252
14 72 56 24 21  252
15 96 75 33 32  384

[[3]]
     N   A   F   M genx
1  252 209  81  84  972
2  180 137  54  59  648
3  216 172  66  69  792
4  216 166  59  65  708
5  264 226  92  93 1104
6  300 246 106  93 1116
7   96  74  29  28  336
8  216 171  68  70  816
9  228 183  74  75  888
10 252 207  83  77  924
11 204 166  65  63  756
12 216 169  69  67  804
13 252 195  74  79  888
14 252 200  88  89 1056
15 384 297 118 119 1416

[[4]]
      N    A   F   M genx
1   972  799 313 316 3756
2   648  523 213 217 2556
3   792  646 259 256 3072
4   708  575 242 231 2772
5  1104  888 372 351 4212
6  1116  913 365 353 4236
7   336  272 114 100 1200
8   816  650 250 262 3000
9   888  718 285 296 3420
10  924  741 302 291 3492
11  756  603 260 245 2940
12  804  637 249 245 2940
13  888  720 288 281 3372
14 1056  847 343 353 4116
15 1416 1111 438 446 5256

[[5]]
      N    A    F    M  genx
1  3756 3021 1229 1232 14748
2  2556 2057  830  793  9516
3  3072 2463 1009  982 11784
4  2772 2214  884  884 10608
5  4212 3385 1355 1361 16260
6  4236 3380 1329 1368 15948
7  1200  986  376  400  4512
8  3000 2407  966  960 11520
9  3420 2702 1091 1090 13080
10 3492 2778 1130 1133 13560
11 2940 2361  932  952 11184
12 2940 2289  932  903 10836
13 3372 2680 1045 1054 12540
14 4116 3279 1305 1304 15648
15 5256 4233 1713 1715 20556

>


On Jan 29, 2008 2:23 PM, Adriana Bejarano <bejaranoac at gmail.com> wrote:
> Hi,
>
>
>
> I have written the following code which works fine
>
>
>
> step<-5
>
> numSim<-15
>
>
>
> N<-double(numSim)
>
> A<-double(numSim)
>
> F<-double(numSim)
>
> M<-double(numSim)
>
> genx<-double(numSim)
>
>
>
> for (i in 1:numSim) {
>
>
>
> N[i]<-20
>
> PN<-(runif(N[i], 0, 1))
>
> A[i]<-sum(ifelse(PN>0.2, 1, 0))
>
>
>
> PF<- runif((A[i]*0.5), 0, 1)
>
> F[i]<-sum(ifelse(PF>0.2, 1, 0))
>
>
>
> PM<- runif((A[i]*0.5), 0, 1)
>
> M[i]<-sum(ifelse(PM>0.2, 1, 0))
>
>
>
> genx[i]<- (min(F[i], M[i])*12)
>
>
>
> }
>
>
>
> data<-data.frame(N, A, F, M, genx)
>
>
>
> What I would like to do next is to make 'genx' from step '1' the seed for N
> in step '2'- run the above code 15 times (numSim), make 'genx' from step '2'
> the seed for N in step '3'- run the above code 15 times (numSim) etc…
>
>
>
> How do I do that?. Any advice would be greatly appreciated
>
>
>
> Adriana (R-beginner)
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list