Blog Archives

Using Boost’s foreach macro

January 30, 2013
By
Using Boost’s foreach macro

Boost provides a macro, BOOST_FOREACH, that allows us to easily iterate over elements in a container, similar to what we might do in R with sapply. In particular, it frees us from having to deal with iterators as we do with std::for_each and std::transform. The macro is also compatible with the objects exposed by Rcpp. Side note: C++11 has introduced...

Read more »

Using Boost’s foreach macro

January 30, 2013
By
Using Boost’s foreach macro

Boost provides a macro, BOOST_FOREACH, that allows us to easily iterate over elements in a container, similar to what we might do in R with sapply. In particular, it frees us from having to deal with iterators as we do with std::for_each and std::transform. The macro is also compatible with the objects exposed by Rcpp. Side note: C++11 has introduced...

Read more »

Quick conversion of a list of lists into a data frame

January 22, 2013
By
Quick conversion of a list of lists into a data frame

Data frames are one of R’s distinguishing features. Exposing a list of lists as an array of cases, they make many formal operations such as regression or optimization easy to represent. The R data.frame operation for lists is quite slow, in large part because it exposes a vast amount of functionality. This sample shows one way to write a much...

Read more »

Quick conversion of a list of lists into a data frame

January 22, 2013
By
Quick conversion of a list of lists into a data frame

Data frames are one of R’s distinguishing features. Exposing a list of lists as an array of cases, they make many formal operations such as regression or optimization easy to represent. The R data.frame operation for lists is quite slow, in large part because it exposes a vast amount of functionality. This sample shows one way to write a much...

Read more »

Passing user-supplied C++ functions

January 21, 2013
By
Passing user-supplied C++ functions

Baptiste asked on StackOverflow about letting users supply C++ functions for use with Armadillo / RcppArmadillo. This posts helps with an extended answer. There is nothing specific about Armadillo here, this would the same way with Eigen, the GSL or any other library a user wants to support (and provides his or her own as<>() and wrap() converters...

Read more »

Passing user-supplied C++ functions

January 21, 2013
By
Passing user-supplied C++ functions

Baptiste asked on StackOverflow about letting users supply C++ functions for use with Armadillo / RcppArmadillo. This posts helps with an extended answer. There is nothing specific about Armadillo here, this would the same way with Eigen, the GSL or any other library a user wants to support (and provides his or her own as<>() and wrap() converters...

Read more »

Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

January 20, 2013
By
Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

Consider the following matrix nr <- nc <- 6 set.seed <- 123 m <- matrix(sample(c(rep(0,9), 1),nr*nc, replace=T), nrow=nr, ncol=nc) sum(m)/length(m) 0.1667 dimnames(m) <- list(letters, letters) m a b c d e f a 0 0 0 0 0 1 b 0 0 0 1 0 1 c 0 0 0 0 0 0 d 0 0 0 0 0 0 e 1 1 0 0 0 0 f 0...

Read more »

Robust Estimators of Location and Scale

January 20, 2013
By
Robust Estimators of Location and Scale

First, the median_Rcpp function is defined to compute the median of the given input vector. It is assumed that the input vector is unsorted, so a copy of the input vector is made using clone and then std::nth_element is used to access the nth sorted element. Since we only care about accessing one sorted element of the vector...

Read more »

Custom as and wrap converters example

January 20, 2013
By
Custom as and wrap converters example

The RcppBDT package interfaces Boost.Date_Time with R. Both systems have their own date representations—and this provides a nice example of custom as<>() and wrap() converters. Here, we show a simplified example. We start with the forward declarations: #include <RcppCommon.h> #include <boost/date_time/gregorian/gregorian_types.hpp> // Gregorian calendar types, no I/O namespace Rcpp { // 'date' class boost::gregorian::date // ...

Read more »

Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

January 20, 2013
By
Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

Consider the following matrix nr <- nc <- 6 set.seed <- 123 m <- matrix(sample(c(rep(0,9), 1),nr*nc, replace=T), nrow=nr, ncol=nc) sum(m)/length(m) 0.1667 dimnames(m) <- list(letters, letters) m a b c d e f a 0 0 0 0 0 1 b 0 0 0 1 0 1 c 0 0 0 0 0 0 d 0 0 0 0 0 0 e 1 1 0 0 0 0 f 0...

Read more »