【问题标题】:Rcpp extract row of a DataFrame [duplicate]Rcpp提取DataFrame的行[重复]
【发布时间】:2014-06-24 15:26:50
【问题描述】:

是否有任何简单的语法可以在 RCpp 中提取 DataFrame 的行(仅包含 numeric)作为 DataFrame 类的一部分或其他一些类?

目前我一直在做以下事情:

DataFrame myFunc(DataFrame& x) {
  ...

  // Suppose I need to get the 10th row
  int nCols=x.size();
  std::vector<double> y;
  y.reserve(nCols);
  for (int j=0; j<nCols;j++) {
    y.push_back((as<vector<double> >(x[j]))[9]); // index in C++ starts at 0
  }

  ...
}

【问题讨论】:

标签: r rcpp


【解决方案1】:

没有数据框行这样的东西,它只是虚拟存在。所以你所拥有的和你应该做的非常接近。但是,您应该使用NumericVector 而不是std::vector&lt;double&gt;,这将几乎一无所获地复制列中的所有数据。

更新伪代码:

DataFrame myFunc(DataFrame& x) {
    ...

    // Suppose I need to get the 10th row
    int nCols=x.size();
    NumericVector y(nCols);
    for (int j=0; j<nCols;j++) {
        NumericVector column = x[j] ;
        y[i] = column[9] ;
    }

    ...
}       

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多