<div dir="ltr">Here is one way to do it in R. Much of the code is checking that the format of 'x'<div>is what you describe and you may omit it if you know that does not need checking.</div><div>The call to 'unlist' in the call to lapply is responsible for setting the classes of the outputs.</div><div><br></div><div>Since translation from R to Rcpp is "seamless" I will leave that to you.</div><div><br></div><div>First an example:</div><div><div> > X <- list(list(factor("a",levels=letters[1:5]), 1+1i, FALSE), </div><div> list(factor("b",levels=letters[1:5]), 2+2i, TRUE))</div><div> > f(X, check=TRUE)</div><div> [[1]]</div><div> [1] a b</div><div> Levels: a b c d e</div><div><br></div><div> [[2]]</div><div> [1] 1+1i 2+2i</div><div><br></div><div> [[3]]</div><div> [1] FALSE TRUE</div></div><div><br></div><div>Then the function:</div><div><div><br></div><div><div>f <- function(x, check = FALSE) {</div><div> if (check) {</div><div> stopifnot(</div><div> is.list(x),</div><div> all(vapply(x, is.list, FUN.VALUE=FALSE)),</div><div> length(unique(vapply(x, length, FUN.VALUE=0)))==1)</div><div> }</div><div> listLength <- length(x)</div><div> if (listLength == 0) {</div><div> return(list())</div><div> }</div><div> elementLength <- length(x[[1]])</div><div> xMatrix <- array(unlist(x, recursive=FALSE), dim=c(elementLength, listLength))</div><div> if (check) {</div><div> # TODO: this rejects mixtures of numerics and integers,</div><div> # but accepts mixtures of factors with different levels.</div><div> elementSignature <- function(e) paste(collapse=",", unlist(lapply(e, class)))</div><div> stopifnot(length(unique(vapply(x, elementSignature, FUN.VALUE="")))==1) </div><div> }</div><div> lapply(seq_len(nrow(xMatrix)), function(i)unlist(xMatrix[i,]))</div><div>}</div></div><div><br></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">Bill Dunlap<br>TIBCO Software<br>wdunlap <a href="http://tibco.com" target="_blank">tibco.com</a></div></div>
<br><div class="gmail_quote">On Fri, May 1, 2015 at 7:28 PM, Tim Keitt <span dir="ltr"><<a href="mailto:tkeitt@utexas.edu" target="_blank">tkeitt@utexas.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>If I have data like</div><div><br></div><div>list(list('a', 1, T), list('b', 2, F), ...)</div><div><br></div><div>(list of lists with same types)</div><div><br></div><div>and I want to convert to</div><div><br></div><div>list(c('a', 'b'), c(1, 2), c(T, F))</div><div><br></div><div>(list of vectors)</div><div><br></div><div>where the types are not known in advance (its trivial if you know the sequence of types in advance).</div><div><br></div><div>Anyone know how to do that in Rcpp?</div><div><br></div><div>THK</div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div><div dir="ltr"><a href="http://www.keittlab.org/" target="_blank">http://www.keittlab.org/</a></div></div>
</font></span></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">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" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>