<div dir="ltr">What about using "Rf_length"? <div><br></div><div>-------------------------------------------------------</div><div>#include <Rcpp.h><br></div><div><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>IntegerVector c_listlengths(List L) {<br> IntegerVector lens(L.size());<br><br> for (int i=0; i<L.size(); i++) {<br> SEXP x = L[i];<br> lens(i) = Rf_length(x);<br> }<br> return lens;<br>}<br></div><div>----------------------------------------------------------</div><div><br></div><div>In R:</div><div><br></div><div>> Rcpp::sourceCpp("example.cpp")<br>> l=list(1:3, 2:3, 1:6)<br>> l2=list(letters[1:3], letters[2:3], letters[1:6])<br>> c_listlengths(l)<br>[1] 3 2 6<br>> c_listlengths(l2)<br>[1] 3 2 6<br>> l3=list(letters[1:6], letters[2:3], letters[1:3])<br>> c_listlengths(l3)<br>[1] 6 2 3<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jul 1, 2021 at 5:14 PM Dr Gregory Jefferis <<a href="mailto:jefferis@mrc-lmb.cam.ac.uk">jefferis@mrc-lmb.cam.ac.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Dear RcppUsers,<br>
<br>
I feel like there must be a simple way to do the equivalent of<br>
<br>
sapply(L, length)<br>
<br>
in Rcpp but I can't seem to get past the problem of converting the <br>
elements of L into a type for which .size() or .length() are valid. I <br>
see that I could do this using some long switch(TYPEOF(x)) statement (eg <br>
<a href="https://gallery.rcpp.org/articles/rcpp-return-macros/" rel="noreferrer" target="_blank">https://gallery.rcpp.org/articles/rcpp-return-macros/</a>):<br>
<br>
// [[Rcpp::export]]<br>
IntegerVector c_listlengths(List L) {<br>
IntegerVector lens(L.size());<br>
<br>
for (int i=0; i<L.size(); i++) {<br>
SEXP x = L[i];<br>
switch (TYPEOF(x)) {<br>
case INTSXP: {<br>
lens(i)=as<IntegerVector>(x).size();<br>
}<br>
// handle other SXP types<br>
}<br>
}<br>
return lens;<br>
}<br>
<br>
but that that seems horribly convoluted. Can anyone point me to a <br>
simpler way?<br>
<br>
Apologies if this is an FAQ, but I did not yet manage to turn up <br>
anything close enough.<br>
<br>
With many thanks,<br>
<br>
Greg.<br>
<br>
# R example<br>
l=list(1:3, 2:3, 1:6)<br>
l2=list(letters[1:3], letters[2:3], letters[1:6])<br>
sapply(l, length)<br>
sapply(l2, length)<br>
<br>
<br>
<br>
--<br>
Gregory Jefferis<br>
Division of Neurobiology<br>
MRC Laboratory of Molecular Biology<br>
Francis Crick Avenue<br>
Cambridge Biomedical Campus<br>
Cambridge, CB2 OQH, UK<br>
<br>
<a href="http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis" rel="noreferrer" target="_blank">http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis</a><br>
<a href="http://jefferislab.org" rel="noreferrer" target="_blank">http://jefferislab.org</a><br>
<a href="https://www.zoo.cam.ac.uk/research/groups/connectomics" rel="noreferrer" target="_blank">https://www.zoo.cam.ac.uk/research/groups/connectomics</a><br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>
<br>
<a href="https://nyquistdata.com/" target="_blank"><img src="https://device.nyquistdata.com/images/logo/logo.svg"></a> <a href="https://www.linkedin.com/company/nyquistdata/" target="_blank"><img src="https://device.nyquistdata.com/images/logo/LinkedIN_black.svg"></a> <a href="https://twitter.com/NyquistData" target="_blank"><img src="https://device.nyquistdata.com/images/logo/Twitter_black.svg"></a>