首页 > 解决方案 > Rcpp如何在数据框中将DoubleVector转换为IntegerVector

问题描述

out构建了一个数据框。假设数据框中的第一列是使用Rcpp::DoubleVector和构造的REAL()。我想将整个列转换为Rcpp::IntegerVector(很像as.integer()在基础 R 中所做的)。

我试过了

out[0] = Rcpp::as<Rcpp::IntegerVector>(out[0]);

但 R 在转换时返回错误。

有没有办法进行转换?

标签: rrcpp

解决方案


这对我来说很好:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
void toInt(DataFrame& x) {
  x[0] = as<IntegerVector>(x[0]);
}

/*** R
toInt(iris)
typeof(iris[[1]])
*/

推荐阅读