Skip to content

Instantly share code, notes, and snippets.

@aaronwolen
Created July 2, 2015 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronwolen/0c2f7af17d20dd396db6 to your computer and use it in GitHub Desktop.
Save aaronwolen/0c2f7af17d20dd396db6 to your computer and use it in GitHub Desktop.
Benchmark for converting matrixes to tidy data.frames in R

library(microbenchmark)

set.seed(1024) nr <- 1e4 nc <- 100

m <- matrix(runif(nr * nc), nrow = nr, dimnames = list(paste0("g", seq_len(nr)), paste0("s", seq_len(nc))))

microbenchmark( m1 = as.data.frame.table(m),

m2 = as.data.frame.table(m, stringsAsFactors = FALSE),

m3 = data.frame(col = rep(colnames(m), each = nrow(m)), row = rep(rownames(m), ncol(m)), value = as.vector(m)),

m4 = data.frame(col = rep(colnames(m), each = nrow(m)), row = rep(rownames(m), ncol(m)), value = as.vector(m), stringsAsFactors = FALSE) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment