基于另一个数据帧的两行比较

时间:2016-04-14 07:57:39

标签: r

让我们从主要数据集开始:

> dput(tbl_test1)
structure(list(X1 = structure(c(17L, 14L, 20L, 16L, 1L, 2L, 3L, 
4L, 15L, 8L, 9L, 10L, 11L, 12L, 13L, 21L, 22L, 23L, 18L, 19L, 
5L, 6L, 7L), .Label = c("Astra_1", "Astra_2", "Astra_3", "Astra_4", 
"Audi_1", "Audi_2", "Audi_3", "BMW_1", "BMW_2", "BMW_3", "BMW_4", 
"BMW_5", "Fiat_1", "Mazda_2", "Mercedes_1", "Nexia_1", "Porsche_1", 
"Scania_1", "Scania_2", "Tico_1", "VW_1", "VW_2", "VW_3"), class = "factor"), 
    X2 = structure(c(2L, 3L, 10L, 7L, 8L, 12L, 9L, 14L, 11L, 
    4L, 5L, 6L, 15L, 13L, 4L, 5L, 9L, 14L, 11L, 1L, 3L, 10L, 
    16L), .Label = c("Astra_1", "Astra_3", "Astra_4", "Audi_1", 
    "Audi_2", "Audi_3", "BMW_1", "BMW_2", "Mazda_2", "Mercedes_1", 
    "Nexia_1", "Porsche_1", "Scania_2", "Tico_1", "VW_2", "VW_3"
    ), class = "factor"), AUC_1 = c(5860133.702, 1296009.939, 
    333123.4932, 250348.9407, 1376193.334, 4080502.863, 3777603.233, 
    3503973.487, 99101538.62, 231873.8462, 87258.75465, 147430.9913, 
    1028986.892, 1451482.832, 8136.72382, 25311.41683, 131352.7137, 
    565410.8186, 30196.23792, 70184.82268, 2526321.019, 381643.2138, 
    819687.9824), AUC_2 = c(4849720.322, 928980.4715, 320547.6185, 
    223287.2029, 1340641.323, 4720329.699, 4369150.434, 3371021.243, 
    108591253.3, 266489.7601, 85384.84604, 165726.7626, 1052130.559, 
    1470876.65, 9499.927679, 49309.74984, 138482.765, 444600.7911, 
    25132.73714, 55453.67019, 2038911.81, 422559.3293, 1445477.433
    ), ratio = c(1.20834467, 1.395088463, 1.03923247, 1.121196994, 
    1.02651866, 0.864452935, 0.864608186, 1.039439753, 0.91261069, 
    0.87010415, 1.021946618, 0.889602795, 0.978003046, 0.98681479, 
    0.856503765, 0.513314647, 0.948513078, 1.271726974, 1.201470327, 
    1.265647926, 1.2390536, 0.90317072, 0.567070757), Country = structure(c(1L, 
    1L, 2L, 4L, 6L, 6L, 6L, 6L, 5L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 
    7L, 7L, 9L, 9L, 3L, 3L, 3L), .Label = c("France", "Germany", 
    "Ireland", "Italy", "Norway", "Poland", "Spain", "Sweden", 
    "Ukraine"), class = "factor")), .Names = c("X1", "X2", "AUC_1", 
"AUC_2", "ratio", "Country"), class = "data.frame", row.names = c(NA, 
-23L))

我想通过查看另一张桌子来比较前两列的汽车。为了更详细地解释,让我把下一个表格放在一边:

> dput(tbl_test2)
structure(list(X = structure(c(17L, 14L, 20L, 16L, 1L, 2L, 3L, 
8L, 9L, 10L, 11L, 12L, 4L, 15L, 13L, 21L, 22L, 23L, 18L, 19L, 
5L, 6L, 7L), .Label = c("Astra_1", "Astra_2", "Astra_3", "Astra_4", 
"Audi_1", "Audi_2", "Audi_3", "BMW_1", "BMW_2", "BMW_3", "BMW_4", 
"BMW_5", "Fiat_1", "Mazda_2", "Mercedes_1", "Nexia_1", "Porsche_1", 
"Scania_1", "Scania_2", "Tico_1", "VW_1", "VW_2", "VW_3"), class = "factor"), 
    X10 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X34 = c(0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 1L), X59 = c(0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    1L, 1L), X84 = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1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L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 
    0L, 0L, 0L), X652 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 
    0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), 
    X733 = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X818 = c(0L, 
    0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 0L, 0L), X896 = c(0L, 0L, 0L, 0L, 0L, 
    1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L), X972 = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 
    0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), 
    X1039 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L)), .Names = c("X", 
"X10", "X34", "X59", "X84", "X110", "X134", "X165", "X199", "X234", 
"X257", "X362", "X433", "X506", "X581", "X652", "X733", "X818", 
"X896", "X972", "X1039"), class = "data.frame", row.names = c(NA, 
-23L))

上表存储了第一列中前一个数据集中的汽车,下一列中我们有两个数字:01。 我想在第一个数据中添加新列,指示第一列中的汽车是否为Porsche_1(全名是否重要。我的意思是_)和来自第二列的汽车{{1数据集编号为2的同一列中的数字为1.这不正确,因此我们在此附加列中放入0或NA。如果两者都在Astra_3Porsche_1的同一列中排名第一 - 则应将数字1放入附加列中。

Desired out:最后一列的数字是随机生成的!

Astra_1

2 个答案:

答案 0 :(得分:3)

实现这一目标的一种方法是使用mapply并检查在X中具有第一个汽车名称的行中是否有1 tbl_test2(第一列除外)是对于包含X中第二个汽车名称的行(也就是检查是否为1列,我们有any),您还会TRUE & TRUE或{{TRUE每对1}},然后您可以使用FALSE转换为0/1

as.integer

修改

如果您想知道匹配的列号,可以使用:

tbl_test1$Comp <- mapply(function(x, y) as.integer(any(unlist(tbl_test2[tbl_test2$X==x, -1]) & unlist(tbl_test2[tbl_test2$X==y, -1]))), 
                         x=as.character(tbl_test1$X1), 
                         y=as.character(tbl_test1$X2))

head(tbl_test1)
#           X1         X2        AUC_1        AUC_2     ratio Country Comp
#1   Porsche_1    Astra_3  5860133.702 4.849720e+06 1.2083447  France    0
#2     Mazda_2    Astra_4  1296009.939 9.289805e+05 1.3950885  France    0
#3      Tico_1 Mercedes_1   333123.493 3.205476e+05 1.0392325 Germany    1
#4     Nexia_1      BMW_1   250348.941 2.232872e+05 1.1211970   Italy    0
#5     Astra_1      BMW_2  1376193.334 1.340641e+06 1.0265187  Poland    0
#6     Astra_2  Porsche_1  4080502.863 4.720330e+06 0.8644529  Poland    0

答案 1 :(得分:2)

使用dplyr,您可以按行计算第二个data.frame中与1中的名称对应的行的匹配X1的数量X2,然后查找是否至少有一个匹配项:

library(dplyr)
tbl_test1 %>% rowwise() %>% 
  mutate(nmatch = sum(which(tbl_test2[grep(X1, tbl_test2$X),]==1) 
                      %in% which(tbl_test2[grep(X2, tbl_test2$X),]==1)),
         Comp = as.integer(nmatch!=0))

#            X1         X2      AUC_1       AUC_2     ratio Country nmatch  Comp
#        (fctr)     (fctr)      (dbl)       (dbl)     (dbl)  (fctr)  (int) (int)
# 1   Porsche_1    Astra_3  5860133.7   4849720.3 1.2083447  France      0     0
# 2     Mazda_2    Astra_4  1296009.9    928980.5 1.3950885  France      0     0
# 3      Tico_1 Mercedes_1   333123.5    320547.6 1.0392325 Germany      2     1
# 4     Nexia_1      BMW_1   250348.9    223287.2 1.1211970   Italy      0     0
# 5     Astra_1      BMW_2  1376193.3   1340641.3 1.0265187  Poland      0     0
# 6     Astra_2  Porsche_1  4080502.9   4720329.7 0.8644529  Poland      0     0
# 7     Astra_3    Mazda_2  3777603.2   4369150.4 0.8646082  Poland      0     0
# 8     Astra_4     Tico_1  3503973.5   3371021.2 1.0394398  Poland      0     0
# 9  Mercedes_1    Nexia_1 99101538.6 108591253.3 0.9126107  Norway      1     1
# 10      BMW_1     Audi_1   231873.8    266489.8 0.8701041  Sweden      0     0
# ..        ...        ...        ...         ...       ...     ...    ...   ...

注意: nmatch列只是一个奖励,你可以一步到位,当然只有Comp