Skip to content

Instantly share code, notes, and snippets.

@reisner
Last active March 28, 2019 18:20
Show Gist options
  • Save reisner/996b7d4f9a36d6488a60e0ff686aa826 to your computer and use it in GitHub Desktop.
Save reisner/996b7d4f9a36d6488a60e0ff686aa826 to your computer and use it in GitHub Desktop.
# Comparing different ways of processing a data frame row-by-row:
library(data.table)
df = data.frame(a = runif(10000), b = runif(10000))
system.time(rbindlist(purrrlyr::by_row(df, function(row) { data.frame(a = row$a) }, .labels = FALSE)$.out))
# user system elapsed
# 4.479 0.008 4.494
# This is fast, but note that 'apply' doesnt preserve types. e.g. character columns will get converted to numeric.
system.time(rbindlist(apply(df, 1, function(row) { data.frame(a = row[['a']]) })))
# user system elapsed
# 0.857 0.004 0.864
system.time(plyr::adply(df, .margins = 1, .fun = function(row) { data.frame(c = row$a) }))
# user system elapsed
# 2.175 0.012 2.197
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment