rcpp

Indexing slice from 3D Rcpp NumericVector

浪尽此生 提交于 2019-12-04 20:17:56
Hi I have what I think must be a really simple Rcpp question regarding treating NumericVector objects as multidimensional arrays. I can't find an answer to what might be obvious. Apologies up front if this is the case -- my inexperience with C++ is to blame... If I use the answer posted here a ( Constructing 3D array in Rcpp ) as an example library("Rcpp") cppFunction(code=' NumericVector arrayC(NumericVector input, IntegerVector dim) { input.attr("dim") = dim; return input; } ') How do I extract/access an single slice / row / column out of the "intput" object? I.e. Do something like

Rcpp equivalent for rowsum [closed]

一曲冷凌霜 提交于 2019-12-04 19:09:43
I am looking for a fast alternative for the R function rowsum in C++ / Rcpp / Eigen or Armadillo. The purpose is to get the sum of elements in a vector a according to a grouping vector b . For example: > a [1] 2 2 2 2 2 2 2 2 2 2 > b [1] 1 1 1 1 1 2 2 2 2 2 > rowsum(a,b) [,1] 1 10 2 10 Writing a simple for loop in Rcpp is very slow, but maybe my code was just inefficient. I tried also to call the function rowsum in Rcpp , however, rowsum is not very fast. Here's my attempt at doing this using Rcpp (first time using the package, so do point out my inefficiencies): library(inline) library(Rcpp)

How to determine the class of object stored in SEXP in Rcpp? [duplicate]

随声附和 提交于 2019-12-04 18:47:28
This question already has answers here : Initialize a variable with different type based on a switch statement (2 answers) Closed 3 years ago . //[[Rcpp::export]] int Class_data(SEXP data) { /* return 0 if class of data is matrix else return -1 */ } How to determine the class of data in the above case? If data belongs to matrix class return 0 else return -1. Depending on what type of object you are checking for, there may be a predefined Rf_isWhatever macro, or you may need to use the Rf_inherits(object, "Whatever") macro, both defined in the R source code, and available through the Rcpp

How to access elements of a vector in a Rcpp::List

∥☆過路亽.° 提交于 2019-12-04 18:34:29
问题 I am puzzled. The following compile and work fine: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] List test(){ List l; IntegerVector v(5, NA_INTEGER); l.push_back(v); return l; } In R: R) test() [[1]] [1] NA NA NA NA NA But when I try to set the IntegerVector in the list: // [[Rcpp::export]] List test(){ List l; IntegerVector v(5, NA_INTEGER); l.push_back(v); l[0][1] = 1; return l; } It does not compile: test.cpp:121:8: error: invalid use of incomplete type 'struct SEXPREC' C:

Template for class returning elements of Rcpp::Vector<RTYPE>

雨燕双飞 提交于 2019-12-04 18:22:11
Consider the following class that does absolutely nothing: class MyNumVec { private: const NumericVector& x; public: MyNumVec(const NumericVector& y) : x(y) {} double operator[](int i) const { // here return x[i]; } operator NumericVector() const { return x; } }; I would like to make it more general and use template for it so that I'll be working with any Vector<RTYPE> instead of numeric vectors only, but the problem is the line marked by comment since I must declare output type. I tried using auto type from C++11, but it doesn't work (" 'auto' return without trailing return type "). How can

Why is my Rcpp implementation for finding the number of unique items slower than base R?

☆樱花仙子☆ 提交于 2019-12-04 16:53:01
I'm trying to write a function to count the number of unique items in a string vector (my problem is slightly more complex, but this is reproducible. I did this based on answers I found for C++. Here is my code: C++ int unique_sort(vector<string> x) { sort(x.begin(), x.end()); return unique(x.begin(), x.end()) - x.begin(); } int unique_set(vector<string> x) { unordered_set<string> tab(x.begin(), x.end()); return tab.size(); } R: x <- paste0("x", sample(1:1e5, 1e7, replace=T)) microbenchmark(length(unique(x)),unique_sort(x), unique_set(x), times=3) Results: Unit: milliseconds expr min lq mean

Multiplying a column vector by a numeric scalar in RcppArmadillo

僤鯓⒐⒋嵵緔 提交于 2019-12-04 15:59:03
I am having some trouble compiling this simple c++ code using Rcpp and the RcppArmadillo package. Take the following simple example to multiply each column of a matrix by a numeric scalar: code <- 'arma::mat out = Rcpp::as<arma::mat>(m); for(int i = 0; i < out.n_cols; ++i){ out.col(i) *= v; } return Rcpp::wrap( out );' Trying to compile this using... require( RcppArmadillo ) armMult <- cxxfunction( signature( m = "numeric" , v = "numeric" ), code , plugin = "RcppArmadillo" ) Results in the compile error.... #error: no match for 'operator*=' in 'arma::Mat<eT>::col(arma::uword) [with eT = double

R: dplyr solution for for-loop with initial conditions set

别来无恙 提交于 2019-12-04 14:27:22
问题 I have a data which has 40 days of the year and some data set.seed(123) df <- data.frame(day = 1:40,rain = runif(40,min = 0, max = 3), petc = runif(40, min = 0.3, max = 8),swc = runif(40, min = 27.01, max = 117.43)) I want to calculate another variable called aetc for each day which is calculated as follows: SW.ini <- 2 # setting some initial values SW.max <- 5 SW.min <- 0 For day 1, 1) Determine a variable called PAW(day1) = SW.ini + rain(day1) 2) If PAW(day1) >= SWC(day1), aetc(day1) = petc

How to slice Rcpp NumericVector for elements 2 to 101?

跟風遠走 提交于 2019-12-04 14:16:44
Hi I'm trying to slice Rcpp's NumericVector for elements 2 to 101 in R, I would do this: array[2:101] How do I do the same in RCpp? I tried looking here: http://gallery.rcpp.org/articles/subsetting/ But the resource has an example that lists all the elements using IntegerVector::create(). However, ::create() is limited by the number of elements. (in addition to being tedious). Any way to slice a vector given 2 indices? This is possible with Rcpp 's Range function. This generates the equivalent C++ positional index sequence. e.g. Rcpp::Range(0, 3) would give: 0 1 2 3 Note: C++ indices begin at

Rcpp: Recommended code structure when using data frames with Rcpp (inline)

淺唱寂寞╮ 提交于 2019-12-04 14:13:26
[I had this sketched out as a comment elsewhere but decided to create a proper question...] What is currently considered "best practice" in terms of code structuring when using data frames in Rcpp? The ease with which one can "beam over" an input data frame from R to the C++ code is remarkable, but if the data frame has n columns, is the current thinking that this data should be split up into n separate (C++) vectors before being used? The response to my previous question on making use of a string (character vector) column in a data frame suggests to me that yes, this is the right thing to do.