Skip to content

Instantly share code, notes, and snippets.

@jsams
Last active September 12, 2018 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsams/8c1dc3a98ed407f8fbecd5b351ea75f6 to your computer and use it in GitHub Desktop.
Save jsams/8c1dc3a98ed407f8fbecd5b351ea75f6 to your computer and use it in GitHub Desktop.
help charlie
library(data.table)
dt = data.table(i=rep(1:5, each=10), t=rep(1:5, times=2), x=runif(50), key=c('i', 't'))
myfunc = function(x) {
return(data.frame(sum=sum(x), mean=mean(x)))
}
dt[, myfunc(x), by=.(i, t)]
myfunc2 = function(df) {
setDF(df)
return(data.frame(sum=sum(df$x), mean=mean(df$x)))
}
dt[, myfunc2(.SD), by=.(i, t)]
myfunc3 = function(dt) {
df = data.frame(dt)
return(data.frame(sum=sum(df$x), mean=mean(df$x)))
}
dt[, myfunc3(.SD), by=.(i, t)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment