[R] for loop query
Gerard M. Keogh
GMKeogh at justice.ie
Tue Dec 9 12:43:18 CET 2008
Hi all,
apologies if this is obvious - but I can't see it and would appreciate some
quick help!
the matrix mhouse is 26x3 and I'm computing odds ratios. The simple code
below "should" compute the odds vector for every pair (325) i.e. 26C2 in
cols 1 and 2.
On the first i=1 outer loop the inner j loop runs from 2 to 26 ok and then
I get the error (Error: subscript out of bounds)
Why isn't my loop incrementing i - the outer loop to 2 and then resetting
j=3?
Am I missing something obvious?
thanks
Gerard
> mhouse
[,1] [,2] [,3]
[1,] 275 819 49
[2,] 593 1323 192
[3,] 813 1181 292
[4,] 2177 5189 1320
[5,] 1651 2243 270
[6,] 1061 5629 11035
[7,] 1690 2302 589
[8,] 1130 1203 345
[9,] 565 1898 655
[10,] 580 730 234
[11,] 343 1761 73
[12,] 372 536 67
[13,] 666 1713 397
[14,] 382 918 279
[15,] 486 921 247
[16,] 1141 988 313
[17,] 626 1135 666
[18,] 438 436 168
[19,] 425 691 101
[20,] 609 716 99
[21,] 467 661 141
[22,] 879 1373 79
[23,] 444 1101 130
[24,] 459 898 351
[25,] 995 1801 398
[26,] 396 1107 201
> # set up the odds vector by declaring it to be null
> odds=NULL
>
> # compute the odds ratios for Individual House vs Scheme House
> for (i in 1:25)
+ {
+ for (j in i+1:26)
+ {
+ todds = (mhouse[i,1]*mhouse[j,2])/(mhouse[j,1]*mhouse[i,2])
+ # compute the todds for row i with row j:
j>i
+ odds = c(odds,todds)
+ # append todds to the odds vector
+ }
+ }
Error: subscript out of bounds
> odds
[1] 0.7491244 0.4877622 0.8003391 0.4561745 1.7814132 0.4573697
0.3574670 1.1279674 0.4226138 1.7239078 0.4838053
[12] 0.8636384 0.8069156 0.6363150 0.2907502 0.6087939 0.3342421
0.5459312 0.3947703 0.4752623 0.5244818 0.8326321
[23] 0.6569199 0.6077702 0.9386447
>
**********************************************************************************
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. It is the policy of the Department of Justice, Equality and Law Reform and the Agencies and Offices using its IT services to disallow the sending of offensive material.
Should you consider that the material contained in this message is offensive you should contact the sender immediately and also mailminder[at]justice.ie.
Is le haghaidh an duine nó an eintitis ar a bhfuil sí dírithe, agus le haghaidh an duine nó an eintitis sin amháin, a bheartaítear an fhaisnéis a tarchuireadh agus féadfaidh sé go bhfuil ábhar faoi rún agus/nó faoi phribhléid inti. Toirmisctear aon athbhreithniú, atarchur nó leathadh a dhéanamh ar an bhfaisnéis seo, aon úsáid eile a bhaint aisti nó aon ghníomh a dhéanamh ar a hiontaoibh, ag daoine nó ag eintitis seachas an faighteoir beartaithe. Má fuair tú é seo trí dhearmad, téigh i dteagmháil leis an seoltóir, le do thoil, agus scrios an t-ábhar as aon ríomhaire. Is é beartas na Roinne Dlí agus Cirt, Comhionannais agus Athchóirithe Dlí, agus na nOifígí agus na nGníomhaireachtaí a úsáideann seirbhísí TF na Roinne, seoladh ábhair cholúil a dhícheadú.
Más rud é go measann tú gur ábhar colúil atá san ábhar atá sa teachtaireacht seo is ceart duit dul i dteagmháil leis an seoltóir láithreach agus le mailminder[ag]justice.ie chomh maith.
***********************************************************************************
More information about the R-help
mailing list