<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div>Hi Rcpp folks;</div><div>I am very new to c++ so I apologize in advance if this is a stupid question:</div><div>For a larger analysis I am trying to implement the R function sample() in c++ via RcppArmadillo. I want to use the resulting vector as an index to access a matrix. A simple dummy example of what I am trying to do is below. By breaking the code into several pieces I have been able to determine that the problem lies with the vector produced by sample() ('guys' in the example below) and the way I am attempting to use it as an index for the matrix tmpMat. I have tried making 'um', 'unmarked' and 'guys' integers/IntegerVectors but that didn't resolve my problem either. <br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style:
normal;">The function compiles fine but when I run it in R I get this nondescript error message: 'Error in fun(y, n, J, lam, um) :'<br></div><div>Any help on this would be greatly appreciated.</div><div>Thanks,</div><div>Rahel</div><div><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">library(inline)<br>library(RcppArmadillo)<br>noccs<-5<br>umn<-20<br>J=15<br>y<-list(matrix(0, nrow=umn, ncol=J),matrix(0, nrow=umn, ncol=J),matrix(0, nrow=umn, ncol=J),<br>matrix(0, nrow=umn, ncol=J),matrix(0, nrow=umn, ncol=J) )<br>n<-5<br>um=c(1:20)<br>lam <-rep(0.3, umn)</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif;
background-color: transparent; font-style: normal;">code<-'Rcpp::Environment base("package:base");<br>Rcpp::Function sample = base["sample"];<br><br>Rcpp::List obs = y;<br>int ntraps = Rcpp::as<int>(J); <br>int npics= Rcpp::as<int>(n);<br>arma::vec probs= Rcpp::as<arma::vec>(lam);<br>arma::vec unmarked = Rcpp::as<arma::vec>(um);<br>bool txtx = FALSE;<br><br>for (int t=0; t<5; t++ ) {<br>arma::mat tmpMat = Rcpp::as<arma::mat>(obs(t));<br><br> for (int k=0; k<ntraps; k++ ) {<br><br> probs= probs/sum(probs);<br> arma::vec guys= Rcpp::as<arma::vec>(sample(unmarked, npics,_["replace"] = txtx, probs));<br> <br> for (int kk=0; kk<npics; kk++){<br>
tmpMat(guys(kk,0),k)=1;<br> }<br> <br> }<br> obs(t)=tmpMat;<br> }<br><br>return Rcpp::wrap(obs);<br>'</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">fun<-cxxfunction(signature(y="list", J="integer",n="integer", lam="numeric",<br> um="numeric"), body=code, plugin="RcppArmadillo" )<br>xx<-fun(y,J,n,lam,um)<br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"></div></div></body></html>