Rcpp实战

        Rcpp比传统的R API简洁易用,更加类似C++了。Rcpp采用的Robject管理R传来的对象,如: cpp::NumericVector ab = Rcpp::NumericVector::create(123.45, 67.89);创建一个2个值的数值型向量,可以直接用下标操作[]取到元素值。

        Rcpp类型和R类型对照表:左边是Rcpp对象,右边是R对象,比如Rcpp::IntegerVecto在R中可以对应1:10

                                Rcpp class                                                                         R 类型
                        Integer(Vector|Matrix)                                            integer vectors and matrices
                        Numeric(Vector|Matrix)                                         numeric ...
                        Logical(Vector|Matrix)                                           logical ...
                        Character(Vector|Matrix)                                      character ...
                        Complex(Vector|Matrix)                                        complex ...
                        List                                                                            list (aka generic vectors) ...
                        Expression(Vector|Matrix)                                    expression ...
                        Environment                                                            environment
                        Function                                                                   function
                        XPtr                                                                           externalptr
                        Language                                                                language
                        S4                                                                              S4

        Rcpp对象转为R对象:template <typename T> SEXP wrap(const T& object);    //将Rcpp中对象转换为R对象,如将Rcpp中的int转为R中的numeric通常用于Rcpp返回值如return Rcpp:wrap(10)。wrap 的作用是将 C++ 的数据类型转换成 R 的数据类型,方便将C++ 的运算结果返回到 R 中

        R的对象转换为Rcpp对象:template <typename T> T as(SEXP m_sexp);     //as 的作用将 R 中的数据类型转换成 C++ 中的数据类型

        Rcpp::Function

        Rcpp::Environment

        Rcpp::Formula

        Rcpp::Language

例如:

#include <Rcpp.h>
#include<iostream>
#include <boost/array.hpp>//这里没有使用boost,只是想说明可以直接使用boost
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
SEXP traverse(NumericMatrix x) {//NumericVector traverse(NumericMatrix x)
  cout<<"矩阵操作"<<endl;
  int nrow=x.nrow();//行数
  int ncol=x.ncol();//列数
  for(int i=0;i<nrow;i++){
    for(int j=0;j<ncol;j++){
      cout<<x(i,j)<<" ";//矩阵下标操作x(i,j)
    }
    cout<<endl;
  }
  cout<<"调用R内置函数"<<endl;
  Environment stats("package:stats");
  Function rnorm=stats["rnorm"];
  //If you don't know in advance what the output will be, 
  //store it in an RObject or in components of a List.
  //特别是tapply这样的函数返回类型是list
  NumericVector a=as<NumericVector>(rnorm(3));//as的用法
  for(NumericVector::iterator it=a.begin();it!=a.end();it++){
    *it+=10;
  }
  a.names() = CharacterVector::create("a", "b", "c");//修改a的属性
  a.attr("my-attr") = "my-value";
  a.attr("class") = "my-class";
  return Rcpp::wrap(a);//可以是return a相当于定义返回值为NumericVector//wrap的用法
}
//下面是R脚本
/*** R
#格式非得这样:/*** R  
x=matrix(1:16,4,4)
traverse(x)
*/


Rcpp中的缺失值:

  int int_s = NA_INTEGER;
  String chr_s = NA_STRING;
  bool lgl_s = NA_LOGICAL;
  double num_s = NA_REAL;

比较好的几个Rcpp教程:

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

http://cran.rstudio.com/web/packages/Rcpp/vignettes/Rcpp-quickref.pdf

http://www.jstatsoft.org/v40/i08/paper

http://cos.name/wp-content/uploads/2012/05/13-huangjinshan-Rcpp.pdf

一个例子,当然这个可以直接在C++中做,但是我数据在R里处理能直接通过Rcpp做的话当然方便。

文件数据部分大致是这样的:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值