且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用单个行替换data.table中的行集

更新时间:2023-12-05 14:22:04

res = setkey(DT[, {
  w = setDT(shift(place, 0:2, type="lead"))[.("b","c","d"), on=.(V1,V2,V3), which=TRUE, nomatch=0]
  if (length(w)){
    w2 = c(w, w + 1L)
    rbind(
      .SD[-w2],
      copy(.SD[w])[, place := "z"]  
    )
  } else .SD
}, by=id], id, seq)

给出

   id place seq
1:  1     a   1
2:  1     z   2
3:  1     d   4
4:  2     a   1
5:  2     b   2
6:  2     d   3
7:  2     e   4

使用对序列b,c,d的连接找到位置w.从那里,我们确定要删除的行(w加上其后的一行);保留哪些行(w);以及要在其中进行哪些修改(位置:="z").

Positions w are found using a join against the sequence b, c, d. From there, we identify which rows to drop (w plus the one after it); which rows to keep (w); and what to modify in them (place := "z").

可以对此进行概括的方向太多了,因此,如果出现更复杂的情况,***只发布一个新问题.

There are too many different directions in which this might be generalized, so probably better to just post a new question if a more complicated case comes up.