如何根据来自具有匹配索引的另一个data.frame的逻辑值从data.frame中删除值?

时间:2016-09-27 19:27:03

标签: r dataframe

我知道有一种方法可以用基础R或dplyr或类似的方法来做到这一点,但我无法解决这个问题......

我有一个data.frame,p,数据:

      TRIAL PCRIT_15min_fishMO2 PCRIT_15min_segmented PCRIT_25bins_fishMO2   PCRIT_25bins_segmented
3  Pcrit_3a              13.326                16.530               13.712                     NA
33 Pcrit_38               5.610                14.832                5.568                 15.091
50 Pcrit_59                  NA                    NA                4.421                  3.129

具有相同尺寸的逻辑data.frame j

      TRIAL PCRIT_15min_fishMO2 PCRIT_15min_segmented PCRIT_25bins_fishMO2 PCRIT_25bins_segmented
3  Pcrit_3a               FALSE                 FALSE                FALSE                     NA
33 Pcrit_38               FALSE                 FALSE                FALSE                  FALSE
50 Pcrit_59                  NA                    NA                 TRUE                   TRUE

我想删除pj中的等效值为FALSE的值。我怎么能这样做?

我想要的结果是:

      TRIAL PCRIT_15min_fishMO2 PCRIT_15min_segmented PCRIT_25bins_fishMO2 PCRIT_25bins_segmented
3  Pcrit_3a                  NA                    NA                   NA                     NA
33 Pcrit_38                  NA                    NA                   NA                     NA
50 Pcrit_59                  NA                    NA                4.421                  3.129

这是我的数据:

p = structure(list(TRIAL = c("Pcrit_3a", "Pcrit_38", "Pcrit_59"), 
PCRIT_15min_fishMO2 = c(13.326, 5.61, NA), PCRIT_15min_segmented = c(16.53, 
14.832, NA), PCRIT_25bins_fishMO2 = c(13.712, 5.568, 4.421
), PCRIT_25bins_segmented = c(NA, 15.091, 3.129)), .Names = c("TRIAL", 
"PCRIT_15min_fishMO2", "PCRIT_15min_segmented", "PCRIT_25bins_fishMO2", 
"PCRIT_25bins_segmented"), row.names = c(3L, 33L, 50L), class = "data.frame")

j = structure(list(TRIAL = c("Pcrit_3a", "Pcrit_38", "Pcrit_59"), 
PCRIT_15min_fishMO2 = c(FALSE, FALSE, NA), PCRIT_15min_segmented = c(FALSE, 
FALSE, NA), PCRIT_25bins_fishMO2 = c(FALSE, FALSE, TRUE), 
PCRIT_25bins_segmented = c(NA, FALSE, TRUE)), .Names = c("TRIAL", 
"PCRIT_15min_fishMO2", "PCRIT_15min_segmented", "PCRIT_25bins_fishMO2", 
"PCRIT_25bins_segmented"), row.names = c(3L, 33L, 50L), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

你的答案是

p[j == FALSE] <- NA

我确信你的问题必须以某种形式在论坛的其他地方得到回答。

干杯

答案 1 :(得分:0)

或者你可以这样做:

p[-1][!j[-1]] <- NA