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

Antonio Piccolboni antonio at piccolboni.info
Mon May 4 06:29:09 CEST 2015


Check here
<https://github.com/RevolutionAnalytics/rmr2/blob/master/pkg/src/t-list.cpp>
for something similar to Tim's solution that preallocates all vectors to
avoid the costly push_back. Still needs the unlists in R, so it's expensive
in that dimension, the number of lists in the output.


Antonio

On Sun, May 3, 2015 at 4:22 PM, Tim Keitt <tkeitt at utexas.edu> wrote:

> 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/
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150503/66952b76/attachment-0001.html>


More information about the Rcpp-devel mailing list