Add R code, compare the version with C++ code