R - 在 Rcpp 中使用原始函数,如 max()、sum() [英] R - use primitive functions like max(), sum() in Rcpp

查看:48
本文介绍了R - 在 Rcpp 中使用原始函数,如 max()、sum()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector FFF(){
NumericVector LB(3);
LB[0]=Language("max",12.3,1.2,13.3,34,10,12.45).eval();
LB[1]=Language("min",12.31,1.24,13.35,340,109,121.45).eval();
LB[2]=Language("sum",12.37,1.21,13.43,34).eval();
return LB;
}

不会通过编译器,因为 "Language("max",12.3,1.2,13.3,34,10,12.45).eval())" 返回 SEXP 对象,它不适合 LB[0]'s 类型双".我真的很想直接使用 R 基础中的 max()、min() 和 sum(),而不是编写额外的 C++ 函数.你有什么好主意吗?

won't pass the compiler, since "Language("max",12.3,1.2,13.3,34,10,12.45).eval())" returns SEXP object, which doesn't fit LB[0]'s type "double". I really want to directly use max(), min() and sum() from the R base instead of writing additional C++ functions. Do you have any good idea?

谢谢!

推荐答案

这是 Rcpp Sugar 的完美用例

http://dirk.eddelbuettel.com/code/rcpp/Rcpp-sugar.pdf

http://adv-r.had.co.nz/Rcpp.html#rcpp-糖

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector FFF(){
  NumericVector LB(3);
  LB[0] = max(NumericVector::create(12.3,1.2,13.3,34,10,12.45));
  LB[1] = min(NumericVector::create(12.31,1.24,13.35,340,109,121.45));
  LB[2] = sum(NumericVector::create(12.37,1.21,13.43,34));
  return LB;
}

这篇关于R - 在 Rcpp 中使用原始函数,如 max()、sum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆