[R] crazy loop error.

Petr Savicky savicky at praha1.ff.cuni.cz
Wed Jan 26 09:18:26 CET 2011

On Tue, Jan 25, 2011 at 08:58:31AM +0000, Prof Brian Ripley wrote:
> >If k may be 0, then it is better to use
> >
> > for (n in seq(length=k))
> >
> >since seq(length=0) has length 0.
> Since you keep mentioning that, it is actually much better to use 
> seq_len(k) (and seq_along(x) instead of your earlier recommendation of 
> seq(along=x)).  And if you are using seq() in other cases in programs, 
> consider seq.int() instead.

Thank you for pointing out the functions seq_len(), seq_along() and
seq.int(). These functions are primitive and faster, as others already
mentioned. Using replicate(), i obtained on my computer a speed up by a
factor between 5 and 7 for k <= 20 and there is a remarkable speed up
also for larger k. The function seq.int() is more general than the other
two. In particular, it can generate also a decreasing sequence.

Petr Savicky.

More information about the R-help mailing list