Skip to content

Instantly share code, notes, and snippets.

// [[Rcpp::depends(RcppGSL)]]
#include <gsl/gsl_randist.h>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector gsl_dexp(NumericVector x) {
int n = x.size();
@kevinushey
kevinushey / magrittr.R
Last active August 29, 2015 13:56
Overloading `|` to act like a pipe operator for data.frames
`|` <- function(x, y) {
if (is.data.frame(x)) {
return( eval(call("%.%", substitute(x), substitute(y)), envir=parent.frame()) )
} else {
return( base::"|"(x, y) )
}
}
library(dplyr)
mtcars |
@kevinushey
kevinushey / R_IsNA.cpp
Last active March 3, 2019 01:29
Comparing methods of checking whether a value is NA
// run me by installing Rcpp and calling sourceCpp on this file
#include <Rcpp.h>
using namespace Rcpp;
// some initial stuff borrowed from R sources
#ifdef WORDS_BIGENDIAN
static const int hw = 0;
static const int lw = 1;
@kevinushey
kevinushey / just_for_fun.R
Created January 31, 2014 03:18
Re: Python and R: Is Python really faster than R?
case2 <- function(n = 10, mu = 3, sigma = sqrt(5), p = 0.025, rep = 100){
xbar <- vapply(1:rep, FUN.VALUE=numeric(1), function(i) {
norm <- rnorm(mean = mu, sd = sigma, n = n)
return(mean(norm))
})
low <- xbar - qnorm(1-p) * (sigma / sqrt(n))
up <- xbar + qnorm(1-p) * (sigma / sqrt(n))
rem <- mu > low & mu < up
@kevinushey
kevinushey / git_status.R
Created January 29, 2014 18:49
A clever use of R's print mechanism for getting the git status for the current directory
git_status <- ''
class(git_status) <- "__git_status__"
print.__git_status__ <- function(x, ...) system("git status")
git_status ## prints 'git status' for the current dir
@kevinushey
kevinushey / enumerate.c
Last active January 3, 2016 06:59
A simple extension of `lapply` to allow passing an index as second argument of the function
// This code copied and modified from the R sources (src/main/apply.c) for do_lapply.
// Essentially, we remove some of the op / arity checking, and then
// construct a call to FUN that also passes the index.
#define USE_RINTERNALS
#include <R.h>
#include <Rinternals.h>
// [[export]]
@kevinushey
kevinushey / NA_punning.cpp
Last active January 1, 2016 16:09
Faster NA checking with punning
#include <Rcpp.h>
using namespace Rcpp;
union DoublePunner {
double d;
uint64_t p;
};
static DoublePunner NA_PUNNED = { NA_REAL };
@kevinushey
kevinushey / Rcpp_ListOf.cpp
Last active March 22, 2016 20:16
Rcpp::ListOf<T> -- An example implementation of a typed List container.
#define debug
#ifdef DEBUG
#define debug(x) Rprintf(x)
#define debug2(x, y) Rprintf(x, y)
#define debug3(x, y, z) Rprintf(x, y, z)
#else
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
@kevinushey
kevinushey / server.R
Created September 17, 2013 21:11
shinyGridster test
library(shiny)
shinyServer( function(input, output, session) {
})
@kevinushey
kevinushey / RcppMelt.md
Last active August 2, 2016 03:13
Implementing reshape2::melt in Rcpp.

Melting with Rcpp

A common data manipulation task is that of making 'wide' data 'long': for example, using the reshape2 library,

library(reshape2)
wide_df &lt;- data.frame( stringsAsFactors=FALSE,