Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
802 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...