# C code programming for statistics

Where can we find C code for statistical computing? R is kind of slow when doing a lot of iterations. C is a lot faster but coding is always heavy-duty. I am wondering does anybody have any good source websites on statistical computing in C. Basically if most of the functions in R, especially matrix manipulations, are rewritten in C code, it will be super nice.

I already know some websites. 1. http://lib.stat.cmu.edu/apstat/

I would appreciate if you can provide me with other similar websites.

• Are you aware that some underlying code in R is actually implemented in C? You can frequently see calls from R to C in the source codes. Also, instead of using loops for iterations it may help formulating your program in vectors and matrices (where that is possible), that should work faster. – Richard Hardy Jan 23 '15 at 20:39
• The Rcpp package for R allows you to implement C directly in your code. There are a lot of packages out there that utilize Rcpp to help with tasks that are generally faster in C, such as 'sliding window' problems. If your use case is specific to matrix operations, I would consider Matlab as a good alternative (which is written in Fortran - which is faster than C) – TLJ Jan 23 '15 at 22:02
• Is the point of looking for C code to be able to program in C, to speed up calculations, to provide interfaces to other platforms, or something else? Incidentally, it is still the case, some 60+ years after people started developing and publishing statistical algorithms, that the sources of many of the best routines are given in Fortran, not C. – whuber Jan 23 '15 at 22:02

There is an introductory statistics book which uses C for everything, including a library written by the author Ben Klemens. It's available as a free online pdf from the author's website:

http://modelingwithdata.org/

I haven't used the C code, but have found parts of the book quite useful, both as an introduction to C and for the statistical topics. I believe it also has some useful info on SQL.

R, if used correctly, should be fast. As mentioned in the comment, R is largely written in C and Fortran and is quite optimized. Make sure you are using the right data structures and leveraging the built in indexing/vector/matrix methods instead of too many loops. You may also want to compile your R code. Check this out for some useful tips:

If you really know what your doing in R and it's still too slow, try switching to NumPy. NumPy's often faster than R, and much easier to do data analysis with than C. I can't recommend trying to write your own version of R in C.

• Avoid loops in R, as others have already suggested. If you can substitute indexing/vector/matrix methods as @ac5 recommends, things speed up a lot. If loops are unavoidable, use the resources for parallel computation in R, like the foreach package. That way you can draw on the wealth of tools available in R without having to start from scratch for performing rapid calculations. – EdM Jan 23 '15 at 21:57
• I'll layer on that IMO if you are new to R, you should immediately start learning w/ the data.table package. – TLJ Jan 23 '15 at 22:06

You might not want to write all your code in C for statistical computing. As THE statistical language, R is the most powerful language for statistical analysis without any doubt. So what you might really want to do is to speed up your R code with some techniques. Some helpful ways are the following.