[Rcpp-devel] Efficient DataFrame access by row & column
Dirk Eddelbuettel
edd at debian.org
Wed Feb 20 00:37:30 CET 2013
Ken,
On 19 February 2013 at 23:24, Ken Williams wrote:
| > From: Dirk Eddelbuettel [mailto:edd at debian.org]
| > Eeeek. I prefer the more C++-y way of writing df(j,i).
|
| Attempting to do so, I get a compile-time error:
|
| window.cpp:68:34: error: ambiguous overload for 'operator-' in 'Rcpp::Vector<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 19, Rcpp::Vector<RTYPE>::Proxy = Rcpp::internal::generic_proxy<19>, size_t = long long unsigned int]((* &((size_t)j)), (* &((size_t)i))) - Rcpp::Vector<RTYPE>::operator()(const size_t&, const size_t&) [with int RTYPE = 19, Rcpp::Vector<RTYPE>::Proxy = Rcpp::internal::generic_proxy<19>, size_t = long long unsigned int]((* &((size_t)j)), (* &((size_t)last_i)))'
| window.cpp:68:34: note: candidates are:
| window.cpp:68:34: note: operator-(SEXP, SEXP) <built-in>
| window.cpp:68:34: note: operator-(SEXP, long long int) <built-in>
| window.cpp:68:34: note: operator-(int, int) <built-in>
|
| For context, the line that's failing is:
|
| if(fabs(df(j,i)-df(j,last_i))>thresh) {
You are relying on an implicit conversion here to make df(.,.) a double. The
compiler tells you that it is trying sugar operators. Sugar is good to have
in Rcpp, but occassionally there is a cost. This may be one of those times.
Try
double a = df(j,i);
double b = df(j,last_i);
if (fabs(a - b) > thresh) {
and see if that works.
You may want to look into using Armadillo vectors and matrices (or Eigen's,
we are equal opportunity here). I had good luck with Armadilllo.
| I was operating under the premise that there "must be" a constant-time accessor for a List element (DataFrame column), and once I have that, a constant-time accessor for an element of that vector. I know the latter is true, but is the former not true? I assumed it was but that I just couldn't find it.
Sadly, "must be" is a little too optimistic.
Not every conceivable operation is implemented in Rcpp, but we are always
open to patches to make it more complete.
Hope this helps, Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list