Skip to content

Instantly share code, notes, and snippets.

@jdesilvio
Last active June 4, 2016 03:41
Show Gist options
  • Save jdesilvio/e5845ca3d9d6f32d4956 to your computer and use it in GitHub Desktop.
Save jdesilvio/e5845ca3d9d6f32d4956 to your computer and use it in GitHub Desktop.
plyr to dplyr
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