Skip to content

[Rcpp-devel] code explanation

3 messages · guillaume chaumet, Kevin Ushey

#
Dear List,
Could I have some explanation comments on this code:
https://github.com/jjallaire/rcpp-gallery/blob/gh-pages/src/2013-01-22-faster-data-frame-creation.cpp
?
I just want to transpose the resulting data frame. However, I want to do it
myself but I don't understand the code.

Thank you for your time

Cheers

Guillaume
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150610/9bb1c738/attachment.html>
#
I found the explanation alone.
The "list to data.frame" conversion was done by simply: returned_frame.attr(
"class") = "data.frame";

This is my solution for a transposed data.frame

## The "R"code for the list:

mylist=list(c("1" , "", "0" , "112.6336"),c("2" , "*" , "20" , "113.0659"),
c("3" , "" , "40" , "111.5833"),c("4" , "" , "60" ,"110.9704"))


// The Cpp code
#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
List CheapDataFrameBuilder(List a) {
    List returned_frame = clone(a);
    StringVector c1(returned_frame.length());
    StringVector c2(returned_frame.length());
    StringVector c3(returned_frame.length());
    StringVector c4(returned_frame.length());
    StringVector vector(4);

    for (int j = 0; j < returned_frame.length(); ++j) {
        vector=returned_frame(j);
        c1(j) = vector(0);
        c2(j) = vector(1);
        c3(j) = vector(2);
        c4(j) = vector(3);
    }
    return Rcpp::DataFrame::create(Rcpp::Named("Frame")=c1,
    Rcpp::Named("Sync")=c2,
    Rcpp::Named("Time")=c3,
    Rcpp::Named("Point_1")=c4);
}

Everything works, however I want to convert the first, the third and the
forth vector into a NumericVector.
I have try:
NumericVector c1=as<NumericVector>(c1)
from Hadley website but:
error: redefinition of 'c1' with a different type: 'Vector<14>' vs
'Vector<16>'

Did I miss something?

Cheers

G


2015-06-10 12:45 GMT+02:00 guillaume chaumet <guillaumechaumet at gmail.com>:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150610/30aa222c/attachment.html>
#
c1 is already defined, so you simply want

    c1 = as<NumericVector>(c1);

That is, when you write

    NumericVector c1 = as<NumericVector>(c1);

you are effectively trying to create a new NumericVector called `c1` -- but
since an object with the name `c1` already exists in scope, that fails.

Kevin

On Wed, Jun 10, 2015 at 11:09 AM guillaume chaumet <
guillaumechaumet at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150610/eb6af770/attachment.html>