r - Rcpp如何在数据框中将DoubleVector转换为IntegerVector
问题描述
我out
构建了一个数据框。假设数据框中的第一列是使用Rcpp::DoubleVector
和构造的REAL()
。我想将整个列转换为Rcpp::IntegerVector
(很像as.integer()
在基础 R 中所做的)。
我试过了
out[0] = Rcpp::as<Rcpp::IntegerVector>(out[0]);
但 R 在转换时返回错误。
有没有办法进行转换?
解决方案
这对我来说很好:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void toInt(DataFrame& x) {
x[0] = as<IntegerVector>(x[0]);
}
/*** R
toInt(iris)
typeof(iris[[1]])
*/
推荐阅读
- python - 我将如何修复此 pkg_resources.DistributionNotFound:未找到 'pygmt' 发行版,应用程序需要该发行版
- java - 在 android studio 中使用 TextChangeHandler 计算
- python - 是否有修复脚本中混合列类型的 DtypeWarning 的方法?
- java - 如何解决a.io.InvalidClassException:org.ejml.data.DMatrixRMaj;本地类不兼容:流 classdesc serialVersionUID
- php - 对嵌套对象应用回调
- vb.net - 我需要把它从一个子改写成一个函数
- dns - Windows Server 2019 - IP 和计算机名称的 Ping 不匹配,但应该匹配
- batch-file - 批量获取重定向输出
- printing - 从桌面上传文本文件时。每次我插入出生日期后,我都会收到此错误
- python - 有序 = True 不被尊重。蟒蛇+棉花糖+烧瓶