[18654] in s-news-athena
[S] Eliminating a for loop
daemon@ATHENA.MIT.EDU (Barker, Chris {PHAR~Palo Alto})
Fri Jul 16 18:43:26 1999
Content-Return: allowed
Date: Fri, 16 Jul 1999 15:37:49 -0700
From: "Barker, Chris {PHAR~Palo Alto}" <CHRIS.BARKER@ROCHE.COM>
To: "'s-news@wubios.wustl.edu'" <s-news@wubios.wustl.edu>
Message-Id: <B9E51B9719BDD111AB640000F80197CDBEB738@rplmsem2.pal.roche.com>
Mime-Version: 1.0
Content-Type: text/plain; CHARSET=US-ASCII
I have a dataframe with daily measurements on 500 patients. Each patient is
measured once a day for 21 days, but there are intermittent days where the
value is missing (NA). (Every patient has a measurement on day 1)
I'd like to simply "carry forward" the non-missing measurements from the day
before ('m doing this to preprocess data for some "groupedData" plots I'm
preparing - I'm not planning to analyze this imputed dataset!)
The code below is quite slow (it ran 10 minutes or so) and eventually my
SPLUS runs out of memory (probably because my dataframe also has about 200
additional variables).
I'd very much appreciate any advice on speeding up/vectorizing this code.
I'll summarize and post any replies.
pt.nos <- unique(pt)
for ( j in 1:length( unique(pt)) ) {
temp.frame <- RawData[ pt== pt.nos[j] ,];
for (i in 2:21){
if (!is.na( temp.frame$var.day[i-1] ) & is.na(temp.frame$var.day[i])
) temp.frame$var.day[i] <- temp.frame$var.day[i-1]
}
if (j==1) RawData.imputed <- temp.frame else RawData.imputed <-
rbind.data.frame(RawData.imputed,temp.frame)
}
Chris Barker, Ph.D.
Principal Pharmacoeconomic Statistician
Roche Pharma Business - Palo Alto
(650)-852-3152
-----------------------------------------------------------------------
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