Skip to content

Creating data.frame from c-language

5 messages · Boris Aronshtam, Brian Ripley, Dirk Eddelbuettel

#
On 09/08/2013 22:48, Boris Aronshtam wrote:
Use data.frame() via an eval() call from C.

Or see the code is stats/src/model.c, as part of model.frame.default 
(but I would only do that if speed were essential).
#
On 10 August 2013 at 14:17, Prof Brian Ripley wrote:
| On 09/08/2013 22:48, Boris Aronshtam wrote:
| > I need to create a data.frame from C-language and populate it. I Know how to create lists from C, but I cannot figure out how to create a data.frame. Does anyone have a sample code for creating a data.frame, setting column names, and populating it with data?
| 
| Use data.frame() via an eval() call from C.
|
| Or see the code is stats/src/model.c, as part of model.frame.default 
| (but I would only do that if speed were essential).

Or if C++ is an option for you, below if a six-line Rcpp source example:

R> sourceCpp("/tmp/dataframe.cpp")   ## see the file below
R> set.seed(42);  getDataFrame(5)    ## set RNG seed, request a dataframe
          a        b
1  1.370958 0.457742
2 -0.564698 0.719112
3  0.363128 0.934672
4  0.632863 0.255429
5  0.404268 0.462293
R> 
 
The source file used above follows below.

Dirk


#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
DataFrame getDataFrame(int n) {
  NumericVector a = rnorm(n);
  NumericVector b = runif(n);
  return DataFrame::create(Named("a") = a,
			   Named("b") = b);
}
1 day later
#
Dear Professor Brian D. Ripley,

Thank you for your reply. Yes, the speed is essential and data is large (gigabytes). I would like to learn from stats/src/model.c. Where can I find it?

Thanks,
Boris

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
Sent: Saturday, August 10, 2013 6:18 AM
To: Boris Aronshtam
Cc: r-devel at r-project.org
Subject: Re: [Rd] Creating data.frame from c-language
On 09/08/2013 22:48, Boris Aronshtam wrote:
Use data.frame() via an eval() call from C.

Or see the code is stats/src/model.c, as part of model.frame.default (but I would only do that if speed were essential).
#
On 12/08/2013 17:24, Boris Aronshtam wrote:
In the R sources ....