[R] Replacing for loop with apply type function.

Ritwik Sinha ritwik.sinha at gmail.com
Tue Oct 24 02:08:23 CEST 2006


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

x

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



More information about the R-help mailing list