[Rcpp-devel] getting ncol(DF) in Rcpp
Douglas Bates
bates at stat.wisc.edu
Mon Jun 27 19:47:24 CEST 2011
A data.frame in R is a curious object that is really a list of the columns. So
myDF.size()
returns the number of columns.
Try the enclosed R source file.
On Mon, Jun 27, 2011 at 12:30 PM, Silkworth,David J.
<SILKWODJ at airproducts.com> wrote:
> You guys know I am here just to give you a chuckle.
>
> I wanted to build a function passing just a dataframe to Rcpp. In order
> to use this dataframe, I need to know how many columns it has at
> runtime. My attempts at getting this ncol information were thwarted on
> several counts. The Dimension class appears to only work on STL
> containers, which Rcpp::DataFrame is not. I resorted to the Environment
> facility to attempt a feeble-minded RInside, (since I can't understand
> RInside anyway).
>
> Environment base("package:base");
> Function ncol = base["ncol"];
> Rcpp::NumericVector test(1);
> test[0]=ncol(myDF);
>
> This fails to compile with the following error:
> error: cannot convert 'SEXPREC*' to
> 'Rcpp::traits::storage_type<14>::type'
>
> However, just short of sending another single element vector with this
> information as an argument to Rcpp I tried the following, AND IT WORKED!
>
> (My debug technique is to send items back to R for inspection. This is
> just some test code to show that an integer value of myNames.size() will
> be useful as a proxy for ncol(DF) in further code development.)
>
> src <- '
> Rcpp::DataFrame myDF=(arg1);
> Environment base("package:base");
> Function names = base["names"];
> Rcpp::CharacterVector myNames(names(myDF));
> Rcpp::NumericVector ncol(1);
> ncol[0]=myNames.size();
> return(ncol);
> '
>
> fun <- cxxfunction(signature(arg1 = "numeric"),
> src, plugin = "Rcpp")
>
> vec1<-rep(5,5)
> vec2<-c(1:5)
> DF<-data.frame(vec1,vec2)
> test<-fun(DF)
>
> Okay, how's that for a laugher.
>
> In my real case I am using the same dataframe that I needed to clean up
> in my 'redimension' chain. My solution there works quite fine. Now I
> have yet to decompose this dataframe back into vectors and a matrix to
> enable entries to be accessed in Rcpp. But at least I have the
> dimensions for the matrix now.
>
> It takes about 3 seconds for R to extract a matrix based on
> DF[,3:ncol(DF)] on a dataframe with 46,000 rows. I am counting on Rcpp
> code to execute this more efficiently. One could argue that I should
> never have left Rcpp in the first place. But that is another story.
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
library(inline)
library(Rcpp)
src <- '
Rcpp::DataFrame foo(foo_);
return wrap(foo.size());
'
ff <- cxxfunction(signature(foo_ = "data.frame"), src, "Rcpp")
ncol(datasets::trees)
ff(datasets::trees)
More information about the Rcpp-devel
mailing list