简体   繁体   中英

data.table drop key rows and summarize

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM