1

I'm trying to implement (in R is too slow) a for loop in C++ (Rcpp) and I need to filer a dataframe according to the id column and get the whole row(s) for later processing by I keep getting this error:

use of overloaded operator '[]' is ambiguous (with operand types 'NameProxy' (aka 'generic-name_proxy<19>') and 'int')

The problem is I've not been able to find any example on how to do it. What I saw is that people break the dataframe down to vectors but in my case the number of columns can vary. Below my code: (the r loop can be seen in this question R - Replace nested for - Nested lists)

dt = data.table(id = c(1,2,3), A = c('A', 'B', 'C'), B = c('Yesterday', 'Today', 'Tomorrow'))


// [[Rcpp::export]]
Rcpp::List buildFacts(DataFrame  dt) {

   int rows             = dt.nrow();
   NumericVector dpid   = dt["id"];
   for (int idx = 0; idx < rows; idx++) {
      int pos = dpid[idx];

      //DataFrame time = dt[id == pos]$B // this is where the error comes
      DataFrame position = dt["id"][pos]$B;
   }
   return Rcpp::List::create(Named("result") = 1);
}
Leonardo Lanchas
  • 1,616
  • 1
  • 15
  • 37
  • Data.frame or data.table? – F. Privé Apr 03 '18 at 10:13
  • Can you provide an R code that works (even if it is slow) to understand better what you want to achieve here? – F. Privé Apr 03 '18 at 10:18
  • @F.Privé here the example https://stackoverflow.com/questions/49169177/r-replace-nested-for-nested-lists although I think what is relevant here is how to filter the data.table in C++. Once I have that, the rest is easy (should) – Leonardo Lanchas Apr 03 '18 at 10:27
  • As you already mentioned, one should use directly column as vectors. For example, see [this answer](https://stackoverflow.com/a/13778302/6103040). – F. Privé Apr 03 '18 at 11:23
  • 1
    Whart is `$PositionDimension` supposed to be? You can't just make stuff up in C++. – Dirk Eddelbuettel Apr 03 '18 at 12:47
  • @DirkEddelbuettel it's actually col B. I forgot to adapt the real code I'm working with. Anyway, there seems to be a way with arma::find but again the thing is to cast a NumericVector to an arma::uvec – Leonardo Lanchas Apr 03 '18 at 13:11
  • 1
    Dollar indexing is an R construct. Don't copy char-by-char to C++, it does not work that way. – Dirk Eddelbuettel Apr 03 '18 at 13:11
  • Correct. There is where I got stuck. I need the 'B' column for a certain Id. That is, i need to filter by one column to get another. How can that be done? – Leonardo Lanchas Apr 03 '18 at 13:17

0 Answers0