特别是在Rcpp中的模糊过载运算符

时间:2017-10-14 02:42:31

标签: r rcpp

这是一个修订过的问题

在r和Rcpp中我有一个声明为string def;的字符串 我还有一个名为Row_Labels的数据框,其中包含两个字母的字符串,“AA”,“BB”等。 现在我正试着这样做..

#include <Rcpp.h>
#include <string.h>
//using namespace Rcpp;
//using namespace std;


// [[Rcpp::export]]
Rcpp::DataFrame process_Data(Rcpp::DataFrame df,Rcpp::DataFrame Row_Labels, Rcpp::DataFrame Column_Labels){

  Rcpp::Rcout << "Test value from 'cout' " << std::endl;
  Rcpp::Rcout << "Number of rows in df = " << df.nrow() << std::endl;

  std::string abc;
  abc = "test value";
  std::string def;
  def = "zz";

    for(int i = 0; i < Row_Labels.nrow() ; i++)
    {

      def = Row_Labels[i];  // error here

      Rcpp::Rcout << "Row_Labels = " << i;
      Rcpp::Rcout << i << " " << Row_Labels[i] << std::endl; // error here

   }


  return Rcpp::DataFrame::create(Rcpp::_["a"]= df);

}

我收到的错误是use of overload operator'=' is ambiguous (with operand types 'string' (aka 'based_string <char, char traits <char>, allocator <char> >') and 'Proxy' (aka 'generic proxy<19>'))

我感谢您的帮助,并希望此修订更有帮助

1 个答案:

答案 0 :(得分:1)

你有一个非常简单的错误:如果行和列标签的类型为DateFrame,那么你不能像Row_Labels[i];那样进行索引 - 这些不是向量。修复:改为使用向量。这也需要使用length()而不是nrow()。所以下面的编译很好:

#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::DataFrame process_Data(Rcpp::DataFrame df,
                             Rcpp::CharacterVector Row_Labels,
                             Rcpp::CharacterVector Column_Labels){

  Rcpp::Rcout << "Test value from 'cout' " << std::endl;
  Rcpp::Rcout << "Number of rows in df = " << df.nrow() << std::endl;

  std::string abc = "test value";
  std::string def = "zz";

  for(int i = 0; i < Row_Labels.length() ; i++) {
      def = Row_Labels[i];  // error here
      Rcpp::Rcout << "Row_Labels = " << i;
      Rcpp::Rcout << i << " " << Row_Labels[i] << std::endl; // error here
  }
  return Rcpp::DataFrame::create(Rcpp::_["a"]= df);
}

我也收紧并缩短了一点。