[18676] in s-news-athena
[S] Query: How to avoid loop
daemon@ATHENA.MIT.EDU (Arisman Adnan)
Tue Jul 20 10:46:02 1999
Date: Tue, 20 Jul 1999 15:40:58 +0100 (GMT)
From: Arisman Adnan <Arisman.Adnan@newcastle.ac.uk>
Reply-To: Arisman Adnan <Arisman.Adnan@newcastle.ac.uk>
To: s-news@wubios.wustl.edu
Message-Id: <Pine.GSO.3.95-960729.990720141926.6754A-100000@finan.ncl.ac.uk>
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
Dear S+ ers,
I would like to avoid a loop from scoring each cell in a
matrix 'mat' (row x 6) based on some conditions below:
if mat[1,1] is equal to 1 or 2
then score[1,1] <- cut(myrn[1,1], v1)
if mat[1,1] is equal to 3 or 4
then score[1,1] <- cut(myrn[1,1], v2)
if mat[1,1] is equal to 5 or 6
then score[1,1] <- cut(myrn[1,1], v3)
The same condition apply for mat[q,2] and mat[q,3] where q is row.
But, the condition of column 4-6 is based on w1, w2, and w3. Note that
'myrn' is a matrix (row x 6) while v and w are vectors.
The following loop does work, but it takes a long time to run this
program. Could anybody help me to solve this problem please.
myfunction <- function(row)
...
mat <- ....................# a matrix (row x 6)
for(q in 1:row) {
for(r in 1:3) {
if(mat[q, r] == 1 | mat[q, r] == 2) {
score[q, r] <- cut(myrn[q, r], v1)
}
if(mat[q, r] == 3 | mat[q, r] == 4) {
score[q, r] <- cut(myrn[q, r], v2)
}
if(mat[q, r] > 4) {
score[q, r] <- cut(myrn[q, r], v3)
}
}
for(r in 4:6) {
if(mat[q, r] == 1 | mat[q, r] == 2) {
score[q, r] <- cut(myrn[q, r], w1)
}
if(mat[q, r] == 3 | mat[q, r] == 4) {
score[q, r] <- cut(myrn[q, r], w2)
}
if(mat[q, r] > 4) {
score[q, r] <- cut(myrn[q, r], w2)
}
}
Cheers,
Aris,---
Ncl, UK.
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news