获取其他data.table中data.table值的频率并添加到当前data.table

时间:2014-11-29 18:09:00

标签: r data.table

library(data.table)
DT1 <- data.table(id = 1:6, junk = c("T", "U", "V", "X", "Y", "Z"),
                  type = c("A", "B", "B", "B", "A", "C"))
DT2 <- data.table(id = 4:6, junk = c("X", "Y", "Z"),
                  type = c("B", "A", "C"))

即,

> DT1
   id junk type
1:  1    T    A
2:  2    U    B
3:  3    V    B
4:  4    X    B
5:  5    Y    A
6:  6    Z    C

> DT2
   id junk type
1:  4    X    B
2:  5    Y    A
3:  6    Z    C

我想向frequency添加一列DT2,其中列出type中任何给定DT1的出现次数。换句话说,结果应如下所示:

> DT2
   id junk type frequency
1:  4    X    B         3
2:  5    Y    A         2
3:  6    Z    C         1

(这似乎与Check frequency of data.table value in other data.table有些相关,但在这种情况下,这可以通过加入另一个方向来实现。在这种情况下,结果数据表应该基于DT2。)

2 个答案:

答案 0 :(得分:3)

 DT1[,frequency:=.N,by=type]
 setkeyv(DT1, colnames(DT1)[-4])
 DT1[DT2]
 #  id junk type frequency
 #1:  4    X    B         3
 #2:  5    Y    A         2
 #3:  6    Z    C         1

假设您的DT1

 DT1 <- data.table(id = 1:5, junk = c("T", "U", "V", "X", "Y"),
              type = c("A", "B", "B", "B", "A"))

使用上面的代码,给出

 DT1[DT2]
 #   id junk type frequency
 #1:  4    X    B         3
 #2:  5    Y    A         2
 #3:  6    Z    C        NA

答案 1 :(得分:1)

试试吧:

   help<-DT1[,list(frequency=.N),by=type]
   setkey(help, type)
   setkey(DT2, type)
   DT2[help]
   #   type id junk frequency
   #1:    A  5    Y         2
   #2:    B  4    X         3
   #3:    C  6    Z         1