I'm looking for an elegant way to iterate over the key of data.table, drop the rows that have that key, then take a summary over the remaining rows. For example:
mydt <- data.table(cat=c("a","a","b","b","c","c","c"), vals = 1:7)
setkey(mydt,cat)
tmp1 <- mydt[!"a"][,mean(vals)]
tmp2 <- mydt[!"b"][,mean(vals)]
tmp3 <- mydt[!"c"][,mean(vals)]
outdt <- data.table(cat=c("a","b","c"),means=c(tmp1,tmp2,tmp3))
Is there a way to loop over the key and do this elegantly? Thanks.
I think this does it, using more traditional data.table
code:
setkey(mydt,cat)
mydt[, list(means=mean(mydt[!.BY,vals])), by=cat]
# or without needing to key first
mydt[, list(means=mean(mydt[cat != .BY,vals])), by=cat]
# cat means
#1: a 5.0
#2: b 4.2
#3: c 2.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With