Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a data-frame from C to R -

Tags:

c

dataframe

r

I followed this link

Passing a data frame from-to R and C using .call()

to find the way to access an R data frame inside C. My requirement is the opposite of that. I have a tabular data in C and need to create an R data frame object in C and return it on as SEXP.

For simple R vectors and lists creation, I followed something like in this link

http://adv-r.had.co.nz/C-interface.html

But I am still wondering how to create a dataframe and return from C to R. Considering a dataframe is a list, I tried creating a list and passing it on, but it expectedly gets me a list in R and not a dataframe.

Any help would be appreciated.

like image 391
krunalvora Avatar asked May 08 '14 16:05

krunalvora


Video Answer


1 Answers

Obligatory Rcpp example:

// [[Rcpp::export]] 
DataFrame createTwo(){
    IntegerVector v = IntegerVector::create(1,2,3);
    std::vector<std::string> s(3);
    s[0] = "a";
    s[1] = "b";
    s[2] = "c";
    return DataFrame::create(Named("a")=v, Named("b")=s);
}

which will get you a 3x2 data.frame with one char vector and one int vector.

like image 126
Dirk Eddelbuettel Avatar answered Oct 02 '22 10:10

Dirk Eddelbuettel