Skip to content

Instantly share code, notes, and snippets.

@mrdwab
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save mrdwab/b83017e62915586c8f99 to your computer and use it in GitHub Desktop.

Select an option

Save mrdwab/b83017e62915586c8f99 to your computer and use it in GitHub Desktop.
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