C similar to R
Does anyone know of a C library that has some standard probability R
functions like the pattern function? I found this:
http://www.gnu.org/software/gsl/
I was wondering if anyone has experience (how effective it is) and if there are others. Thank.
source to share
You can always embed R in your C application. It's doable and documented, but rather tedious since the API is pretty bare.
If you are open to C ++, this becomes much easier thanks to RInside . If you can do it in R:
R> set.seed(123); sample(LETTERS[1:5], 10, replace=TRUE)
[1] "B" "D" "C" "E" "E" "A" "C" "E" "C" "C"
R>
you can do the same in C ++ quite easily thanks to RInside :
edd@max:~/svn/rinside/pkg/inst/examples/standard$ cat rinside_sample12.cpp
// Simple example motivated by StackOverflow question on using sample() from C
//
// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string cmd = "set.seed(123); sample(LETTERS[1:5], 10, replace=TRUE)";
Rcpp::CharacterVector res = R.parseEval(cmd); // parse, eval + return result
for (int i=0; i<res.size(); i++) {
std::cout << res[i] << " ";
}
std::cout << std::endl;
exit(0);
}
edd@max:~/svn/rinside/pkg/inst/examples/standard$
and given that it executes the same code with the same RNG seed, it also returns the same result:
edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample12
B D C E E A C E C C
edd@max:~/svn/rinside/pkg/inst/examples/standard$
If you just drop the code I showed above into the directory of your examples/standard
existing RInside installation and say the make
executable will be generated and provided with the same base name as the original file (here rinside_sample12
from rinside_sample12.cpp
).
source to share
Googling for C statistics library
, gave me good hits, among others GSL. See also this SO question for more advice. However, I think your best option is to integrate R into your C code. You can do this in two ways:
- Call R through a system call. This is a very simple yet effective option. Especially when there is not a lot of data between R and C, this works really well. Debugging R code from Python was pretty tricky for example.
- Create a form of direct connection inside C to session R. This works really well when there is a lot of data going back and forth between R and C, because everything goes through memory and not disk. However, I predict that it will be more difficult to write than the first solution. See this post for details . ...
source to share