Skip to content

Instantly share code, notes, and snippets.

@kevinushey
kevinushey / strings_with_rcpp.Rmd
Last active December 10, 2015 20:28
Handling Strings with Rcpp
---
title: Handling Strings with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: string vector
summary: Demonstrates how one might handle a vector of strings with `Rcpp`,
in addition to returning output.
---
This is a quick example of how you might use Rcpp to send and receive R
@kevinushey
kevinushey / apply_mean.cpp
Last active April 14, 2019 06:23
row, column-wise operations in Rcpp
// the column-wise implementation is just as fast as colMeans,
// but the row-wise operation is not quite as fast as rowMeans.
// can I do better?
#include <Rcpp.h>
using namespace Rcpp;
template <class T>
inline double do_mean( T& x ) {
@kevinushey
kevinushey / RcppApply.Rmd
Last active November 10, 2022 00:18
Make your own 'apply' functions for matrices
---
title: Make your own 'apply' functions for matrices
author: Kevin Ushey
license: GPL (>= 2)
tags: Rcpp apply matrix
summary: Clever use of sourceCpp can allow you to define your own 'apply' functions on the fly.
---
Make your own 'apply' functions
==========
---
title: Using Boost's foreach macro
author: Kevin Ushey
license: GPL (>= 2)
tags: basics boost
summary: Boost's BOOST_FOREACH can enable a more functional programming style.
---
Boost provides a macro, `BOOST_FOREACH`, that allows us to easily iterate
over elements in a container, similar to what we might
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
StringVector check( StringVector x ) {
Rcout << "The type of x is: " << ::TYPEOF( x ) << std::endl;
Rcout << "The type of x(0) is: " << ::TYPEOF( x(0) ) << std::endl;
Rcout << "The integer associated with STRSXPs is: " << STRSXP << std::endl;
Rcout << "The integer associated with CHARSXPs is: " << CHARSXP << std::endl;
Rcout << "Is x a string?: " << ::Rf_isString(x) << std::endl;
@kevinushey
kevinushey / fast_factor_generation.Rmd
Last active December 14, 2015 06:49
Fast factor generation with Rcpp
---
title: Fast factor generation with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: factor tapply sugar
summary: We can make use of Rcpp sugar to implement a faster factor generator.
---
Recall that factors are really just integer vectors with 'levels',
i.e., character labels that get mapped to each integer in the vector.
@kevinushey
kevinushey / pymat.R
Last active December 15, 2015 12:19
Python style formatting of strings
pymat <- function(string, ...) {
i <- 0
for( arg in list(...) ) {
to_replace <- paste( sep='', "\\{", i, "\\}" )
string <- gsub( to_replace, arg, string )
i <- i + 1
}
return( string )
}
@kevinushey
kevinushey / Rcpp_wrap_and_recurse.Rmd
Last active December 15, 2015 22:28
Dynamic Wrapping and Recursion with Rcpp
---
title: Dynamic Wrapping and Recursion with Rcpp
author: Kevin Ushey
license: GPL (>= 2)
tags: basics
summary: We can use parts of R's API alongside Rcpp to recurse through
lists and dynamically wrap objects as needed.
---
We can leverage small parts of the R's C API in order to
@kevinushey
kevinushey / swap.R
Created May 15, 2013 18:43
A handy function for swapping values in a vector with other values.
swap <- function(vec, from, to) {
tmp <- to[ match(vec, from) ]
tmp[is.na(tmp)] <- vec[is.na(tmp)]
return(tmp)
}
## example
swap( 1:10, c(5, 7), c(50, 70) ) ## exchange occurrences of 5 w/ 50, 7 w/ 70
a <- 1
b <- 2
f <- function(x) {
call <- match.call()
len <- length( call[[2]] )
output <- x
names(output) <- sapply( call[[2]][2:len], as.character )
return(output)
}