s-news
[Top] [All Lists]

[S] Query: How to avoid loop

To: s-news@wubios.wustl.edu
Subject: [S] Query: How to avoid loop
From: Arisman Adnan <Arisman.Adnan@newcastle.ac.uk>
Date: Tue, 20 Jul 1999 15:40:58 +0100 (GMT)
Reply-to: Arisman Adnan <Arisman.Adnan@newcastle.ac.uk>
Sender: owner-s-news@wubios.wustl.edu

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

<Prev in Thread] Current Thread [Next in Thread>
  • [S] Query: How to avoid loop, Arisman Adnan <=