Last active
June 4, 2016 03:41
-
-
Save jdesilvio/e5845ca3d9d6f32d4956 to your computer and use it in GitHub Desktop.
plyr to dplyr
This file contains 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
df <- data.frame(id = seq(1, 500), | |
attr1 = rep(c("up", "up", "down", "down")), | |
attr2 = rep(c("left", "right", "left", "right")), | |
attr3 = rep(c("b", "a", "select", "start")), | |
attr4 = sample(c("c", "o", "n", "t", "r", "a"), 100, replace=T), | |
x = rnorm(100)) | |
dfChar = df | |
dfChar[, names(dfChar)] <- lapply(dfChar[, names(dfChar)], as.character) | |
dt = as.data.table(df) | |
dtChar = as.data.table(dfChar) | |
llply(list(df,dfChar, dt, dtChar), str) | |
system.time(ddply(df, .(attr4), plyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(df, .(attr4), dplyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dfChar, .(attr4), plyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dfChar, .(attr4), dplyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dt, .(attr4), plyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dt, .(attr4), dplyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dtChar, .(attr4), plyr::mutate, l = seq(1, length(attr4)))) | |
system.time(ddply(dtChar, .(attr4), dplyr::mutate, l = seq(1, length(attr4)))) | |
system.time(dt %>% group_by(attr4) %>% mutate(l = seq(1, length(x)))) | |
system.time(a <- ddply(dt, .(attr4), dplyr::mutate, l = seq(1, length(attr4)))) | |
system.time(b <- dt %>% group_by(attr4) %>% mutate(l = seq(1, length(x))) %>% arrange(attr4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment