How to access a specific element of a DataFrame in Rcpp?

52 views Asked by At

With a NumericMatrix, it is easy to access a specific element. For example, we can access the element of row i and col j of the NumericMatrix X with X(i, j). Is there an equivalent operator for a Rcpp::DataFrame?

I am looking for a yes/no answer or a short example.

1

There are 1 answers

3
Ada Lovelace On

I am looking for a yes/no answer or a short example.

The short answer is a no, just as to your question from yesterday. A data.frame is a list of vectors so a homogenous-style accessor cannot work as it does for a Matrix.

A short example of what actually works follows. Note that this appears to have been answered by the DataFrame example in package RcppExamples from which can quote:

#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 return old and new in list
    return List::create(Named("origDataFrame") = DF,
                        Named("newDataFrame") = NDF);
}

It helps to rember that a data.frame really is a list of vectors.