在多列

时间:2016-03-30 11:01:02

标签: r data.table

请在这里找到一个很长的data.table的一小部分。我正在使用

dput(dt)
structure(list(id = 1:15, pnum = c(4298390L, 4298390L, 4298390L, 
    4298558L, 4298558L, 4298559L, 4298559L, 4299026L, 4299026L, 4299026L, 
    4299026L, 4300436L, 4300436L, 4303566L, 4303566L), invid = c(15L, 
    101L, 102L, 103L, 104L, 103L, 104L, 106L, 107L, 108L, 109L, 87L, 
    111L, 2L, 60L), fid = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    4L, 4L, 4L, 4L, 3L, 3L, 2L, 2L), .Label = c("CORN", "DowCor", 
    "KIM", "Texas"), class = "factor"), dom_kn = c(1L, 0L, 0L, 0L, 
    1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L), prim_kn = c(1L, 
    0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L), pat_kn = c(1L, 
    0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L), net_kn = c(1L, 
    0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L), age_kn = c(1L, 
    0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L), legclaims = c(5L, 
    0L, 0L, 2L, 5L, 2L, 5L, 0L, 0L, 0L, 0L, 5L, 0L, 5L, 2L), n_inv = c(3L, 
    3L, 3L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L)), .Names = c("id", 
    "pnum", "invid", "fid", "dom_kn", "prim_kn", "pat_kn", "net_kn", 
    "age_kn", "legclaims", "n_inv"), class = "data.frame", row.names = c(NA, 
    -15L))

我希望在5个不同的列中应用比大于比较的更多

在每个pnum(专利)中,有多个invid(发明人)。我想将每行dom_knprim_knpat_knnet_knage_kn列的值与其他行中的值进行比较相同的pnum。比较只是>,如果值确实比另一个大,那么一个"点"应该归结为。

因此,对于第一行pnum == 4298390invid == 15,您可以看到五列中的值均为1,而invid == 101 | 102的值均为零。这意味着如果我们将第一行中的每个值单独比较(大于?)第二行和第三行中的每个单元格,则总和将为10个点。在每个比较中,第一行中的值更大,并且有10个比较。 比较的数量是设计5 * (n_inv -1)。 我要为第1行寻找的结果应为10 / 10 = 1

对于pnum == 4298558net_knage_kn列的两行中的值均为1(对于invid 103和104),因此每个列应获得0.5分(如果有三个发明者的价值1,每个人应该得到0.33分)。 pnum == 4298558也是如此。

对于下一个pnum == 4299026,所有值都为零,因此每次比较都应该得到0分。

因此注意区别:有三种不同的二元比较

1 > 0 --> assign 1
1 = 1 --> assign 1 / number of positive values in column subset
0 = 0 --> assign 0

期望的结果 data.table中的额外列result,其值为1 0 0 0.2 0.8 0.2 0.8 0 0 0 0 1 0 0.8 0.2

有关如何有效计算这一点的任何建议?

谢谢!

2 个答案:

答案 0 :(得分:6)

vars = grep('_kn', names(dt), value = T)

# all you need to do is simply assign the correct weight and sum the numbers up
dt[, res := 0]
for (var in vars)
  dt[, res := res + get(var) / .N, by = c('pnum', var)]

# normalize
dt[, res := res/sum(res), by = pnum]
#    id    pnum invid    fid dom_kn prim_kn pat_kn net_kn age_kn legclaims n_inv res
# 1:  1 4298390    15   CORN      1       1      1      1      1         5     3 1.0
# 2:  2 4298390   101   CORN      0       0      0      0      0         0     3 0.0
# 3:  3 4298390   102   CORN      0       0      0      0      0         0     3 0.0
# 4:  4 4298558   103 DowCor      0       0      0      1      1         2     2 0.2
# 5:  5 4298558   104 DowCor      1       1      1      1      1         5     2 0.8
# 6:  6 4298559   103 DowCor      0       0      0      1      1         2     2 0.2
# 7:  7 4298559   104 DowCor      1       1      1      1      1         5     2 0.8
# 8:  8 4299026   106  Texas      0       0      0      0      0         0     4 NaN
# 9:  9 4299026   107  Texas      0       0      0      0      0         0     4 NaN
#10: 10 4299026   108  Texas      0       0      0      0      0         0     4 NaN
#11: 11 4299026   109  Texas      0       0      0      0      0         0     4 NaN
#12: 12 4300436    87    KIM      1       1      1      1      1         5     2 1.0
#13: 13 4300436   111    KIM      0       0      0      0      0         0     2 0.0
#14: 14 4303566     2 DowCor      1       1      1      1      1         5     2 0.8
#15: 15 4303566    60 DowCor      1       0      0      1      0         2     2 0.2

处理上述NaN案件(可以说是正确的答案)留待读者阅读。

答案 1 :(得分:1)

以下是使用dplyr的快速解决方案:

library(dplyr)
dt %>%
 group_by(pnum) %>% # group by pnum
 mutate_each(funs(. == max(.) & max(.) != 0), ends_with('kn')) %>%
 #give a 1 if the value is the max, and not 0. Only for the column with kn
 mutate_each(funs(. / sum(.)) , ends_with('kn')) %>%
 #correct for multiple maximums
 select(ends_with('kn')) %>%
 #remove all non kn columns
 do(data.frame(x = rowSums(.[-1]), y = sum(.[-1]))) %>%
 #make a new data frame with x = rowsums for each indvidual
 # and y the colusums
 mutate(out = x/y)
 #divide by y (we could just use /5 if we always have five columns)

out列中提供所需的输出:

Source: local data frame [15 x 4]
Groups: pnum [6]

      pnum     x     y   out
     (int) (dbl) (dbl) (dbl)
1  4298390     5     5   1.0
2  4298390     0     5   0.0
3  4298390     0     5   0.0
4  4298558     1     5   0.2
5  4298558     4     5   0.8
6  4298559     1     5   0.2
7  4298559     4     5   0.8
8  4299026   NaN   NaN   NaN
9  4299026   NaN   NaN   NaN
10 4299026   NaN   NaN   NaN
11 4299026   NaN   NaN   NaN
12 4300436     5     5   1.0
13 4300436     0     5   0.0
14 4303566     4     5   0.8
15 4303566     1     5   0.2

NaN来自没有获胜者的团体,使用例如:

将其转换回来
x[is.na(x)] <- 0