Here is a hint as to how to replace the loops:

> hours <- 0:5
> result <- lapply(hours, function(.hr){
+     cbind(.hr, replicate(5, mean(sample(1:100, 1000, replace=TRUE))))
+ })
> do.call(rbind, result)
      .hr
 [1,]   0 50.232
 [2,]   0 51.135
 [3,]   0 49.169
 [4,]   0 50.264
 [5,]   0 52.621
 [6,]   1 51.852
 [7,]   1 50.557
 [8,]   1 50.501
 [9,]   1 49.710
[10,]   1 49.867
[11,]   2 50.536
[12,]   2 51.562
[13,]   2 51.269
[14,]   2 50.638
[15,]   2 51.083
[16,]   3 49.785
[17,]   3 48.843
[18,]   3 51.156
[19,]   3 50.877
[20,]   3 51.012
[21,]   4 52.214
[22,]   4 50.470
[23,]   4 50.388
[24,]   4 49.600
[25,]   4 51.057
[26,]   5 51.202
[27,]   5 51.022
[28,]   5 52.084
[29,]   5 49.944
[30,]   5 49.946


On Wed, Dec 30, 2009 at 12:16 PM, James Rome <jamesr...@gmail.com> wrote:

> Oops. Thank you.  I guess I needed mn[i*1000 + j]. I should have known
> better.
>
> But I had wanted to make this into a data frame with
> df=data.frame(mn, il)
> so that I could plot it using factors. Can I do that with a matrix?
> And can I eliminate the loops?
>
>
> On 12/30/09 12:11 PM, jim holtman wrote:
>
> First of all your expression 'i*j' is storing on top of one another.  Look
> at what happens in the case of i=1,j=8  i=2,j=4  i=4,j=2, ....  These are
> all storing to the same location.  Instead make 'mn' & 'li' matrices:
>
> mn <- matrix(length(alist), 1000)
>
> and then within the loop:
>
> mn[i,j] <- value
>
> On Wed, Dec 30, 2009 at 11:52 AM, James Rome <jamesr...@gmail.com> wrote:
>
>> Dear kind list people:
>>
>> I have the following code:
>> >hours
>>  [1] "0"  "1"  "2"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13"
>> "14" "15"
>> [16] "16" "17" "18" "19" "20" "21" "22" "23"
>> > alist
>> $`0`
>>  [1]  3 10 10  6  5  6  4  8  9  3  7  5  8  3  6  7  2  6  6  1  4  8
>> 10  4 10
>> [26] 13  6  2  8  4  7  3  4  7  9  6  4  7  4  4  4  3
>>
>> $`1`
>> [1] 1 1 3 2 3 4 2 1
>>
>> $`2`
>> [1] 1 1 3 3
>> . . .
>> mn=c(length(alist)*1000)
>> il=c(length(alist)*1000)
>> # Now calculate the means
>> for(i in 1:length(alist)) {
>>  for(j in 1:1000) {mn[i*j]=mean(sample(alist[[i]], 1000, replace=TRUE));
>> il[i*j]= hours[i]}}
>>
>> But not even the il vector is correct:
>> >il
>>    [1] "0"  "1"  "2"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13"
>> "14"
>>   [15] "15" "16" "17" "18" "19" "20" "21" "22" "23" "12" "5"  "13" "9"
>> "14"
>>   [29] "0"  "15" "0"  "16" "11" "17" "7"  "18" "0"  "19" "13" "20" "0"
>> "21"
>>   [43] "0"  "22" "15" "23" "0"  "16" "7"  "10" "17" "13" "0"  "18" "11"
>> "14"
>>   [57] "19" "1"  "0"  "20" "0"  "1"  "21" "16" "13" "22" "0"  "17" "23"
>> "14"
>>   [71] "0"  "18" "0"  "1"  "15" "19" "11" "13" "0"  "20" "9"  "1"  "0"
>> "21"
>>  . . .
>> and after a while, both mn and il get lots of NAs. The first 1000
>> entries should be "0".
>>
>> What am I doing wrong?
>>
>> And is there a way to do this without the two for loops?
>>
>> Thanks,
>> Jim Rome
>>
>> ______________________________________________
>> R-help@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<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 that you are trying to solve?
>
>


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

What is the problem that you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to