使用data.table或dplyr条件插入行

时间:2017-04-16 11:13:46

标签: r data.table dplyr

我有一个像这样的data.table:

w <- data.table(id=c(rep(1:2,each=4)),
                b=c(rep(1:4,each=1)),
                c1=c(letters[1:8]),
                d=c(LETTERS[1:8]))

现在,我想:

  1. 对于每个id为b&gt; 2
  2. 使c1等于滞后c1
  3. d等于lead d(或NA)
  4. b应为NA
  5. 所需的输出是:

      > w2
        id  b c1  d
     1:  1  1  a  A
     2:  1  2  b  B
     3:  1  3  c  C
     4:  1 NA  b  D
     5:  1  4  d  D
     6:  1 NA  c NA
     7:  2  1  a  A
     8:  2  2  b  B
     9:  2  3  c  C
    10:  2 NA  b  D
    11:  2  4  d  D
    12:  2 NA  c NA
    

1 个答案:

答案 0 :(得分:2)

您可以汇总问题中描述的 data.table ,然后使用rbindlist将其添加到原始 data.table

使用:

# option 1:
w.add <- w[, .(b = NA^(b > 2), 
               c1 = shift(c1, fill = NA, type = 'lag'), 
               d = shift(d, fill = NA, type = 'lead')), 
           id][is.na(b)]

# option 2:
w.add <- w[, .(b = NA^(b > 2)[b > 2], 
               c1 = shift(c1, fill = NA, type = 'lag')[b > 2], 
               d = shift(d, fill = NA, type = 'lead')[b > 2]), 
           id]

rbindlist(list(w, w.add))[order(id)]

给出:

    id  b c1  d
 1:  1  1  a  A
 2:  1  2  b  B
 3:  1  3  c  C
 4:  1  4  d  D
 5:  1 NA  b  D
 6:  1 NA  c NA
 7:  2  1  e  E
 8:  2  2  f  F
 9:  2  3  g  G
10:  2  4  h  H
11:  2 NA  f  H
12:  2 NA  g NA