将StringVector与Rcpp连接 [英] Concatenate StringVector with Rcpp

查看:66
本文介绍了将StringVector与Rcpp连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何用Rcpp连接2个字符串。

I can not figure out how to concatenate 2 strings with Rcpp; and the documentation did not help me while I suspect there is an obvious answer.

http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/

http://gallery.rcpp.org/articles/strings_with_rcpp/

StringVector concatenate(StringVector a, StringVector b)
{
 StringVector c;
 c= ??;
 return c;
}

我期望此输出:

a=c("a","b"); b=c("c","d");
concatenate(a,b)
[1] "ac" "bd"


推荐答案

一种有效的解决方案是使用:

A working solution is to use :

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
CharacterVector concatenate(std::string x, std::string y)
{
               return wrap(x + y);
}

然后:

Vconcatenate=Vectorize(concatenate)
Vconcatenate(letters[1:2],letters[3:4])

或:

// [[Rcpp::export]]
CharacterVector concatenate(std::vector<std::string> x,std::vector<std::string> y)
{
  std::vector<std::string> res(x.size());
  for (int i=0; i < x.size(); i++)
  {
    res[i]=x[i]+y[i];
  }
  return wrap(res);
}

这篇关于将StringVector与Rcpp连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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