Here you will find daily news and tutorials about R, contributed by over 750 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

This short post is to share an R tip Pierre recently gave me. When you need to store values sequentially (typically inside a loop), it’s more far efficient to create the whole vector (or matrix) and to fill it, rather than to concatenate the values to your current vector (or matrix). In terms of allocation time, it’s up to 60 times faster

n=10^5
a=NULLsystem.time(for(i in1:n){a=c(a,1)})
>utilisateur système écoulé
> 12.090.0012.09
b=rep(0,n)system.time(for(i in1:n){b[i]=1})
>utilisateur système écoulé
> 0.210.000.20

which makes a big difference. In a Gibbs sampling loop, where a lot of other computations are done, I still have gain of 25%! For other recommendations (don’t use parentheses where you can avoid them), you can have a look on this post.

Related

To leave a comment for the author, please follow the link and comment on their blog: Statisfaction » R.