6

我试过这个:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector difflag(NumericVector x, int lag) {
    int n = x.size();
    NumericVector out(n-lag);

    for(int i=0; i<(n-lag); i++) {
        out[i] = x[i+lag] - x[i];
    }

    out.attr("class") += "myclass";
    return out;
}

它给了我一个错误:

all.cpp: In function ‘Rcpp::NumericVector difflag(Rcpp::NumericVector, int)’:
all.cpp:64:26: error: no match for ‘operator+=’ in ‘Rcpp::RObject::attr(const string&) const((* & std::basic_string<char>(((const char*)"class"), (*(const std::allocator<char>*)(& std::allocator<char>()))))) += "myclass"’
make: *** [all.o] Error 1
4

1 回答 1

5

也许是这样的:

 CharacterVector classes = out.attr( "class" ) ;
 classes.push_back( "myclass" ) ;
 out.attr( "class" ) = classes ;

可能有通用append功能的空间。

于 2013-11-14T16:13:05.867 回答