Skip to content

Instantly share code, notes, and snippets.

@jsams

jsams/dsetin.R

Last active Sep 12, 2018
Embed
What would you like to do?
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