s-news
[Top] [All Lists]

Summary: Vectorizing 'for' loop?

To: "'s-news@lists.biostat.wustl.edu'" <s-news@lists.biostat.wustl.edu>
Subject: Summary: Vectorizing 'for' loop?
From: "King, Calvin" <cking@sandia.gov>
Date: Thu, 2 May 2002 16:19:19 -0600
Within 16 hours of posting the question, I got 15 responses from the
following individuals: Brad J. Biggerstaff, Nick Ellis, Chuck Taylor, Samuel
Buttrey, S.D. Byers, James Holtman, Jingshan Zhang, Petr Pikal, Bert Gunter,
Andy Liaw, Jean V. Adams, Leonid Gibiansky, David L. Lorenz, Malcolm Hawkes,
and Sundar Dorai-Raj. Thanks to each and everyone of you.

The responses fell neatly into one of three categories: (a) subscripting the
data frame - 6, (b) using "ifelse" - 6, and (c) using "cut" - 4. Since the
solutions in each category were similar, I chose to include only one from
each. Since the first method seemed to be the simplest to me, I decided to
use it in my application.

(a) subscripting the data frame by Brad J. Biggerstaff
myFrame$v11 <- rep("",nrow(myFrame)) # just to set up an empty character
vector
myFrame[myFrame$v1 >= 0 & myFrame$v2 >= 0 & myFrame$v2 < 60,"v11"] <- "NE1"
myFrame[myFrame$v1 >= 0 & myFrame$v2 >= 60 & myFrame$v2 < 120,"v11"] <-
"NE2"
myFrame[myFrame$v1 >= 0 & myFrame$v2 >= 120,"v11"] <- "NE3"
myFrame$v11 <- factor(myFrame$v11)

(b) using "ifelse" by David L. Lorenz
myFrame$v11 <- ifelse(myFrame$v1 >= 0.0 & myFrame$v2 >= 0.0 & myFrame$v2[i]
< 60.0, "NE1",
               ifelse(myFrame$v1 >= 0.0 & myFrame$v2 >= 60.0 & myFrame$v2 <
120.0, "NE2",
               ifelse(myFrame$v1 >= 0.0 & myFrame$v2 >= 120.0, "NE3", NA)))

(c) using "cut" by Jean V. Adams
myFrame$v11 <- cut(myFrame$v2, breaks=c(0, 60, 120, max(myFrame$v2)+1),
     labels=c("NE1", "NE2", "NE3"), factor.result=T, left.include=T)

The more I use S-PLUS, the more I am appreciating its versatility. Needless
to say, this group is very helpful.

>  -----Original Message-----
> From:         King, Calvin  
> Sent: Wednesday, May 01, 2002 4:28 PM
> To:   's-news@lists.biostat.wustl.edu'
> Subject:      Vectorizing 'for' loop?
> 
> I am working with a data frame with about 100k rows and about 10 columns.
> The following "for" loop quits after a while due to insufficient virtual
> memory. I am running S-PLUS 6 on Windows 2000 Professional. How would I
> vectorize this operation? 
> 
>   count <- count.rows(myFrame)
>   for(i in 1:count) {
>       if(myFrame$v1[i] >= 0.0 && myFrame$v2[i] >= 0.0 && myFrame$v2[i] <
> 60.0) { myFrame$v11[i] <- "NE1" }
>       else if(myFrame$v1[i] >= 0.0 && myFrame$v2[i] >= 60.0 &&
> myFrame$v2[i] < 120.0) { myFrame$v11[i] <- "NE2" }
>       else if(myFrame$v1[i] >= 0.0 && myFrame$v2[i] >= 120.0) {
> myFrame$v11[i] <- "NE3" }
>   }
>   myFrame$v11 <- factor(myFrame$v11, levels=c("NE1", "NE2", "NE3"))
> 
> Thanks, in advance.
> 
> Calvin King
> cking@sandia.gov
> 
> 


<Prev in Thread] Current Thread [Next in Thread>