[R] Replacing for loop with apply type function.

From: Ritwik Sinha <ritwik.sinha_at_gmail.com>
Date: Tue 24 Oct 2006 - 00:08:23 GMT

Hi Everyone,

I want to replace the following for loop with an "apply" type function to (possibly) speed up the process.

# The for loop I want to replace

x <- numeric(10)
for(i in 2:10)
x[i] <- x[i-1]+1


To give you some more background, I am running a Metropolis Hastings MCMC chain with k parameters. So, if "x" represents the sample from the chain, the ith row (representing the ith iteration) depends on the (i-1)th row.

Thanks in advance,
Ritwik Sinha
Graduate Student
Epidemiology and Biostatistics
Case Western Reserve University
ritwik.sinha_at_gmail.com | +12163682366 | http://darwin.cwru.edu/~rsinha

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue Oct 24 11:07:42 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 24 Oct 2006 - 01:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.