Skip to content

Instantly share code, notes, and snippets.

@kevinushey
kevinushey / index.html
Created June 18, 2013 03:20
Scatterplot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<script src='http://polychart.com/s/third_party/polychart2.standalone.js' type='text/javascript'></script>
<style>
.rChart {
display: block;
@kevinushey
kevinushey / RcppArmadillo_row_shuffle.cpp
Created August 9, 2013 00:06
RcppArmadillo row shuffling
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::imat shuffle(arma::imat A) {
for (int i=0; i < A.n_rows; ++i) {
A.row(i) = shuffle( A.row(i), 1 );
}
return A;
@kevinushey
kevinushey / .gitignore
Last active December 21, 2015 21:39 — forked from hadley/.gitignore
.Rproj.user
.Rhistory
.RData
*.Rproj
*.html
@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,
@kevinushey
kevinushey / server.R
Created September 17, 2013 21:11
shinyGridster test
library(shiny)
shinyServer( function(input, output, session) {
})
@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 / 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 / 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 / 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 / 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