2014-11-09 46 views
0

我有兩個向量..一個是按組輸出,第二個是對一個組的應用的索引。在實踐中,類似的東西使用輸入矩陣進行索引RcppArmadillo

mean_group = 1,2,3

GROUP_ID = 1,1,3,2,3,2

而且我想給每個ID分配給它的組的值..在基本的R,我只會做mean_group [group_id] ..

我必須避免使用循環,否則,在使用犰狳沒有意義。有沒有辦法做到這一點?

在此先感謝

回答

1

我不知道你怎樣努力找到這個犰狳文檔中,但這個工程出犰狳盒子。請嘗試以下爲文件armaind.cpp

#include <RcppArmadillo.h> 

// [[Rcpp::depends(RcppArmadillo)]] 

// [[Rcpp::export]] 
arma::vec subsetter(arma::vec big, arma::uvec ind) { 
    arma::vec small = big.elem(ind); 
    return small; 
} 

/*** R 
big <- 2*(1:10) 
ind <- c(3,5,7) 
subsetter(big, ind) 
*/ 

它讓你

R> Rcpp::sourceCpp("/tmp/armaind.cpp") 

R> big <- 2*(1:10) 

R> ind <- c(3,5,7) 

R> subsetter(big, ind) 
    [,1] 
[1,] 8 
[2,] 12 
[3,] 16 
R> 

注R和C之間的斷接一個索引差異++。

+0

其實,我知道如何子集..我更有興趣分配... – msdu 2014-11-10 03:06:34