Currently, I am working on my Master thesis using R. My project has a huge data set with many parameters to estimate (over 2000) by using MCMC with complicated likelihood functions (6 layers of for loops). I wrote the code in R and finally I was able to run it. Unfortunately, 1 iteration took a day! Since I need to run it at least 10,000 times for it to get an answer, it would take about 27 years to get the result. No way, I don't want to wait 27 years to graduate! Fortunately, I heard the programming language C is comparatively faster than R and I am starting to learn how to write C.
Since R is written partially in C and partially in Fortran, C code can be called inside R. That is, for complicated calculation problems, using C to do the jobs and returning back the result into R for simpler jobs. Since I am new to C, I think it is easier for me to use R as a platform and use C for the complicated calculation rather than rewrite the whole code in C. However, before calling C code from R, I need to learn the basic knowledge about C so that I can create C function code.
While I am reading a C book, I found out several differences in C compared to R.
- C requires data type: ex) int, double, char, void and so on
- C has "main": kind of a function but special (every program need to have main)
- C has arrays rather than matrix: 1 dimensional array in C is treated as vector in R, 2 dimensional array in C treated as matrix in R, and 3 dimensional array in C is treated as list in R
- C has pointers: pointer holds a variable's address
- C has structures: kind of like an object where variables are grouped together
I am reading a book called "The C programming language" by Brian W. Kernichan and Dennis M. Ritchie. For more information, please read the book.