[Rcpp-devel] List of Lists to List of Vectors

Tim Keitt tkeitt at utexas.edu
Mon May 4 01:22:36 CEST 2015


A slightly improved version:

library(Rcpp)

code = '
SEXP test(List a)
{
  auto l = Rf_length(a[0]);
  using svec = std::vector<SEXP>;
  std::vector<svec> x(l);
  for (List b : a)
  {
    if (b.size() != l)
      stop("Ragged input");
    for (int i = 0; i != l; ++i)
      x[i].push_back(b[i]);
  }
  return wrap(x);
}'

f = cppFunction(code = code, plugins = "cpp11")

res = lapply(f(list(list(T, 1, 'a'),
                    list(F, 2, 'b'))),
             unlist)


On Sun, May 3, 2015 at 11:57 AM, Tim Keitt <tkeitt at utexas.edu> wrote:

> Here's a really bare-bones version. Not very pretty or complete, but it
> does do the job. Could the 'unlist' part be converted to Rcpp?
>
> THK
>
> library(Rcpp)
>
> code = '
> SEXP test(List a)
> {
>   int l = Rf_length(a[0]);
>   typedef std::vector<SEXP> svec;
>   std::vector<svec> x(l);
>   for (int i = 0; i != l; ++i)
>     for (int j = 0; j != a.size(); ++j)
>     {
>       List b = a[j];
>       x[i].push_back(b[i]);
>     }
>   return wrap(x);
> }'
>
> f = cppFunction(code = code)
>
> res = lapply(f(list(list(1, 'a'), list(2, 'b'))), unlist)
>
>
> On Sat, May 2, 2015 at 1:18 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
>>
>> On 2 May 2015 at 10:49, William Dunlap wrote:
>> | Since translation from R to Rcpp is "seamless" I will leave that to you.
>>
>> One problem is with the strongly typed nature of C++.  Rearranging
>> dynamicly
>> growing objects can be done.  I think I used Boost's variant type a few
>> years.  I am sure there are other possibilities.  We should collect a few
>> and
>> compare.   But in C++ please :)
>>
>> Dirk
>>
>> --
>> http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>>
>
>
>
> --
> http://www.keittlab.org/
>



-- 
http://www.keittlab.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150503/7b027b91/attachment.html>


More information about the Rcpp-devel mailing list