Search code examples
rcpp

I have a data frame in which its columns do not have name and I want to name them in RCPP?How can I do that?


I am very new in Rcpp. I have a data frame in which its columns do not have name and I want to name them in Rcpp. How can I do that? That is, this data frame is an input and then I want to name its columns in the first step. Please let me know how I can do that.


Solution

  • Welcome to StackOverflow. We can modify the existing example in the RcppExamples package (which you may find helpful, just as other parts of the Rcpp documentation) to show this.

    In essence, we just reassign a names attribute.

    Code

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    List DataFrameExample(const DataFrame & DF) {
    
        // access each column by name
        IntegerVector a = DF["a"];
        CharacterVector b = DF["b"];
        DateVector c = DF["c"];
    
        // do something
        a[2] = 42;
        b[1] = "foo";
        c[0] = c[0] + 7; // move up a week
    
        // create a new data frame
        DataFrame NDF = DataFrame::create(Named("a")=a,
                                          Named("b")=b,
                                          Named("c")=c);
    
        // and reassign names
        NDF.attr("names") = CharacterVector::create("tic", "tac", "toe");
    
        // and return old and new in list
        return List::create(Named("origDataFrame") = DF,
                            Named("newDataFrame") = NDF);
    }
    
    /*** R
    D <- data.frame(a=1:3,
                    b=LETTERS[1:3],
                    c=as.Date("2011-01-01")+0:2)
    rl <- DataFrameExample(D)
    print(rl)
    */
    

    Demo

    R> Rcpp::sourceCpp("~/git/stackoverflow/61616170/answer.cpp")
    
    R> D <- data.frame(a=1:3,
    +                 b=LETTERS[1:3],
    +                 c=as.Date("2011-01-01")+0:2)
    
    R> rl <- DataFrameExample(D)
    
    R> print(rl)
    $origDataFrame
       a   b          c
    1  1   A 2011-01-08
    2  2 foo 2011-01-02
    3 42   C 2011-01-03
    
    $newDataFrame
      tic tac        toe
    1   1   A 2011-01-08
    2   2 foo 2011-01-02
    3  42   C 2011-01-03
    
    R> 
    

    If you comment the line out you get the old names.