A common data manipulation task is that of making 'wide' data 'long':
for example, using the reshape2 library,
library(reshape2)
wide_df <- data.frame( stringsAsFactors=FALSE,| library(shiny) | |
| shinyServer( function(input, output, session) { | |
| }) |
| .Rproj.user | |
| .Rhistory | |
| .RData | |
| *.Rproj | |
| *.html |
| // [[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; |
| <!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; |
| 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) | |
| } |
| 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 |
| --- | |
| 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 |
| 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 ) | |
| } |
| --- | |
| 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. |