Here is the complete test file. It doesn't need your extractor function and just reassembles the subsets, but for that it needs the newest Rcpp, as it is currently on GitHub, where Kevin seems to have added some work on indexing the subset, which we need here:
#include <Rcpp.h> /*** R ## Suppose I have the data frame below created in R: ## NB: stringsAsFactors set to FALSE ## NB: setting seed as well set.seed(42) myDF <- data.frame(id = rep(c(1,2), each = 5), alph = letters[1:10], mess = rnorm(10), stringsAsFactor=FALSE) */ // [[Rcpp::export]] Rcpp::DataFrame extract(Rcpp::DataFrame D, Rcpp::IntegerVector idx) { Rcpp::IntegerVector id = D["id"]; Rcpp::CharacterVector alph = D["alph"]; Rcpp::NumericVector mess = D["mess"]; return Rcpp::DataFrame::create(Rcpp::Named("id") = id[idx], Rcpp::Named("alpha") = alph[idx], Rcpp::Named("mess") = mess[idx]); } /*** R extract(myDF, c(2,4,6,8)) */
With this file we get the expected result:
R> library(Rcpp) R> sourceCpp("/tmp/sepher.cpp") R> ## Suppose I have the data frame below created in R: R> ## NB: stringsAsFactors set to FALSE R> ## NB: setting seed as well R> set.seed(42) R> myDF <- data.frame(id = rep(c(1,2), each = 5), + alph = letters[1:10], + mess = rnorm(10), + .... [TRUNCATED] R> extract(myDF, c(2,4,6,8)) id alpha mess 1 1 c 0.363128 2 1 e 0.404268 3 2 g 1.511522 4 2 i 2.018424 R> R> packageDescription("Rcpp")$Version ## unreleased version [1] "0.11.1.1" R>
I just needed something similar a few weeks ago (but did not involve character vectors) and used Armadillo with its elem() functions, using the unsigned int vector as an index.
source share