Skip to content
Back to formatted view

Raw Message

Message-ID: <1317808173267-3873945.post@n4.nabble.com>
Date: 2011-10-05T09:49:33Z
From: Chris82
Subject: optimize R code: replace for loop

Dear R Users,

at the moment I am trying to optimize an R script.

testvec <- c(0,1,0,1,1,1,1,0,0,1,0,1,0)


sum.testvec <- vector()
tempsum <- 1
for (e in 1:length(testvec)){
sum.testvec[e] <- tempsum+testvec[e]
tempsum <- sum.testvec[e]

}

final.sum <- c(1,sum.testvec)


Is there an option to do something with apply? Unfortunately I am not so
familiar with the apply functions.

Thanks.

--
View this message in context: http://r.789695.n4.nabble.com/optimize-R-code-replace-for-loop-tp3873945p3873945.html
Sent from the R help mailing list archive at Nabble.com.