How create an Rcpp NumericVector with more than 20 entries?

后端 未结 2 1876
萌比男神i
萌比男神i 2021-01-19 16:21

Creating a NumericVector with more than 20 elements leads to error messages. This is in agreement with this document (at the very bottom): http://statr.me/rcpp-note/api/Vec

2条回答
  •  醉酒成梦
    2021-01-19 17:14

    Create the vector with the size you need then assign the values & names. This is an Rcpp "inline" function (easier for folks to try it out) but it'll work in your context:

    library(Rcpp)
    library(inline)
    
    big_vec <- rcpp(body="
    NumericVector run(26); 
    CharacterVector run_names(26);
    
    # make up some data
    for (int i=0; i<26; i++) { run[i] = i+1; };
    
    # make up some names
    for (int i=0; i<26; i++) { run_names[i] = std::string(1, (char)('A'+i)); };
    
    run.names() = run_names;
    
    return(run);
    ")
    
    big_vec()
    ## A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z 
    ## 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    

提交回复
热议问题