Non-equi join, then summarize by group

牧云@^-^@ 提交于 2021-02-04 18:07:54

问题


Here is a MWE.

dta <- data.table(id=rep(1:2, each=5), seq=rep(1:5, 2), val=1:10)
dtb <- data.table(id=c(1, 1, 2, 2), fil=c(2, 3, 3, 4))
dtc <- data.table(id=c(1, 1, 2, 2), mval=rep(0, 4))
for (ind in 1:4) dtc$mval[ind] <- mean( dta$val [dta$id == dtb$id[ind] & dta$seq < dtb$fil[ind]] )

dtc
#    id      mval
# 1:  1       1.0
# 2:  1       1.5
# 3:  2       6.5
# 4:  2       7.0

dtc should have the same number of rows as dtb. For every (row) ind in dtc,

  1. dtc$id[ind] = dtb$id[ind].
  2. dtc$mval[ind] = mean(dta$val[x]), where x is dta$id == dtb$id[ind] & dta$seq < dtb$fil[ind].

My data.tables are extremely large. Hence, I am looking for a way to achieve the above with minimal memory footprint. I was thinking a non-equi join and then a summarize, but I can't seem to get that to work. Hence, the title of the question.

Would greatly appreciate any help, thanks!


回答1:


May be this helps

dtc[, mval := dta[dtb, mean(val) ,on =.(id, seq < fil), by = .EACHI]$V1]
dtc
#   id mval
#1:  1  1.0
#2:  1  1.5
#3:  2  6.5
#4:  2  7.0


来源:https://stackoverflow.com/questions/39555361/non-equi-join-then-summarize-by-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!