Last active
August 29, 2015 14:07
-
-
Save mrdwab/b83017e62915586c8f99 to your computer and use it in GitHub Desktop.
Functions to benchmark for http://stackoverflow.com/questions/26160079/fast-concise-way-to-generate-ordered-frequency-count-of-unique-matrix-rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(data.table) | |
| library(dplyr) | |
| x <- read.csv(textConnection( | |
| '0,1,1,0 | |
| 0,1,1,0 | |
| 1,0,1,0 | |
| 0,1,0,1 | |
| 1,0,0,1'), | |
| header = FALSE) | |
| set.seed(1) | |
| x20 <- x[sample(nrow(x), 20, TRUE), ] | |
| x1M <- x[sample(nrow(x), 1e6, TRUE), ] | |
| funDplyr <- function(indf) { | |
| temp1 <- indf %>% | |
| mutate(rowid = sequence(nrow(indf))) %>% | |
| regroup(lapply(names(indf), as.symbol)) %>% | |
| mutate(rows = paste(rowid, collapse = ",")) %>% | |
| summarise(N = n(), rowid = rowid[1], rows = rows[1]) %>% | |
| arrange(rowid) | |
| temp1$rows <- strsplit(temp1$rows, ",", fixed = TRUE) | |
| temp1 | |
| } | |
| funDplyr(x) | |
| funDplyr(x20) | |
| funDplyr(x1M) | |
| funDt <- function(indt, addRN = TRUE, rnName = "rn") { | |
| if (!is.data.table(indt)) indt <- as.data.table(indt) | |
| if (isTRUE(addRN)) indt[, (rnName) := sequence(nrow(indt))] | |
| byCols <- setdiff(names(indt), rnName) | |
| indt[, list( | |
| .N, rowid = get(rnName)[1], | |
| rows = list(get(rnName))), | |
| by = byCols] | |
| } | |
| funDt(x) | |
| funDt(x20) | |
| funDt(x1M) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment