rcpp

Passing by reference a data.frame and updating it with rcpp

放肆的年华 提交于 2019-11-29 09:24:49
问题 looking at the rcpp documentation and Rcpp::DataFrame in the gallery I realized that I didn't know how to modify a DataFrame by reference. Googling a bit I found this post on SO and this post on the archive. There is nothing obvious so I suspect I miss something big like "It is already the case because" or "it does not make sense because". I tried the following which compiled but the data.frame object passed to updateDFByRef in R stayed untouched #include <Rcpp.h> using namespace Rcpp; // [

How to get Rcpp to work in R on a Windows XP platform?

我怕爱的太早我们不能终老 提交于 2019-11-29 07:59:36
I've been trying to work with Rcpp in R 2.14.2 on a Windows XP platform. As far as I can tell, I followed all of the recommended steps for getting Rcpp to work: I installed R in a directory called C:\R\R-2.14.2; I installed the latest version of Rtools in the directory C:\R\Rtools; I set the environment PATH to the following (in this exact same order): C:\R\Rtools\bin;C:\R\Rtools\gcc-4.6.3\bin; C:\R\R-2.14.2\bin\i386;C:\WINDOWS;C:\WINDOWS\system32 Despite all of this, when I tried to run a test example in R to see if Rcpp works, I got an error message. Here is the test example: library(Rcpp)

na.locf and inverse.rle in Rcpp

好久不见. 提交于 2019-11-29 07:23:05
I wanted to check if there is any pre-existing trick for na.locf (from zoo package), rle and inverse.rle in RCpp ? I wrote a loop to implement, e.g. I did the implementation of na.locf(x, na.rm=FALSE, fromLast=FALSE) as follows: #include <Rcpp.h> using namespace Rcpp; //[[Rcpp::export]] NumericVector naLocf(NumericVector x) { int n=x.size(); for (int i=1;i<n;i++) { if (R_IsNA(x[i]) & !R_IsNA(x[i-1])) { x[i]=x[i-1]; } } return x; } I was just wondering that since these are quite basic functions, someone might have already implemented them in RCpp in a better way (may be avoid the loop) OR a

RcppArmadillo Compile Errors on OS X Mavericks

匆匆过客 提交于 2019-11-29 07:11:33
This is a follow-up to the question at Element-Wise Matrix Multiplication in Rcpp I have been getting a number of different kinds of errors with RcppArmadillo since upgrading to Mavericks. I have Xcode 5.0.2 and Command Line Tools installed. Also, gfortran from Homebrew. But I keep encountering the error below -- > cppFunction("arma::mat schur(arma::mat& a, arma::mat& b) { return(a % b); }", depends="RcppArmadillo") ld: library not found for -lgfortran clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [sourceCpp_18474.so] Error 1 clang++ -I/Library

Group vector on conditional sum

梦想的初衷 提交于 2019-11-29 06:45:46
I want to group a vector based on the sum of the elements being less than or equal to n . Assume the following, set.seed(1) x <- sample(10, 20, replace = TRUE) #[1] 3 4 6 10 3 9 10 7 7 1 3 2 7 4 8 5 8 10 4 8 #Where, n = 15 The expected output would be to group values while their sum is <= 15, i.e. y <- c(1, 1, 1, 2, 2, 3, 4, 5 ,5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 10) As you can see the sum is never greater than 15, sapply(split(x, y), sum) # 1 2 3 4 5 6 7 8 9 10 #13 13 9 10 15 12 12 13 14 8 NOTE: I will be running this on huge datasets (usually > 150 - 200GB) so efficiency is a must. A method

passing unevaluated expressions to C/C++

狂风中的少年 提交于 2019-11-29 05:55:53
I'd like to pass a variable number of arguments from a function to C/C++, but would like to leave the arguments unevaluated and at the same time don't want to do any computations in R (aside from calling the C/C++ function), i.e. I don't want to call substitute in my R function. One option for this that I thought I could use is .External and doing smth like this: R_fn = function(...) .External("cpp_fn", ...) ... # and in C code: SEXP cpp_fn (SEXP arglist) { } However .External is evaluating arguments in ... , so if I try something like rm(x, y) # just making sure these don't exist R_fn(x*y) I

When does using RNGScope make a difference?

落爺英雄遲暮 提交于 2019-11-29 04:40:33
In Rcpp documentation, I often find the recommendation to place Rcpp::RNGScope scope; before using random draws within Rcpp. I wondered what exactly this does, because I've only ever seen it described as " ensures RNG state gets set/reset ". Then, I tested a bit, but I can't seem to come up with an example where doing this makes any difference. I used an example from here . My tests were: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] NumericVector noscope() { Rcpp::Function rt("rt"); return rt(5, 3); } // [[Rcpp::export]] NumericVector withscope() { RNGScope scope; Rcpp::Function

Fastest way to filter a data.frame list column contents in R / Rcpp

我与影子孤独终老i 提交于 2019-11-29 04:40:19
I have a data.frame: df <- structure(list(id = 1:3, vars = list("a", c("a", "b", "c"), c("b", "c"))), .Names = c("id", "vars"), row.names = c(NA, -3L), class = "data.frame") with a list column (each with a character vector): > str(df) 'data.frame': 3 obs. of 2 variables: $ id : int 1 2 3 $ vars:List of 3 ..$ : chr "a" ..$ : chr "a" "b" "c" ..$ : chr "b" "c" I want to filter the data.frame according to setdiff(vars,remove_this) library(dplyr) library(tidyr) res <- df %>% mutate(vars = lapply(df$vars, setdiff, "a")) which gets me this: > res id vars 1 1 2 2 b, c 3 3 b, c But to get drop the

convert Rcpp::CharacterVector to std::string

妖精的绣舞 提交于 2019-11-29 02:36:47
问题 I am trying to open a file within an Rcpp function, so I need the file name as a char* or std::string. So far, I have tried the following: #include <Rcpp.h> #include <boost/algorithm/string.hpp> #include <fstream> #include <string> RcppExport SEXP readData(SEXP f1) { Rcpp::CharacterVector ff(f1); std::string fname = Rcpp::as(ff); std::ifstream fi; fi.open(fname.c_str(),std::ios::in); std::string line; fi >> line; Rcpp::CharacterVector rline = Rcpp::wrap(line); return rline; } But apparently,

Ordering Permutation in Rcpp i.e. base::order()

依然范特西╮ 提交于 2019-11-29 01:12:35
问题 I have a ton of code using the base::order() command and I am really too lazy to code around that in rcpp. Since Rcpp only supports sort, but not order, I spent 2 minutes creating this function: // [[Rcpp::export]] Rcpp::NumericVector order_cpp(Rcpp::NumericVector invec){ int leng = invec.size(); NumericVector y = clone(invec); for(int i=0; i<leng; ++i){ y[sum(invec<invec[i])] = i+1; } return(y); } It somehow works. If the vectors are containing unique numbers, I get the same result as order(