简体   繁体   English

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

[英]How can I remove values from a data.frame based on logical values from another data.frame with matching indices?

I know there's a way to do this with base R or dplyr or something similar but I just can't figure this out...我知道有一种方法可以使用 base R 或 dplyr 或类似的东西来做到这一点,但我就是想不通......

I have a data.frame, p , with data:我有一个 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

And a logical data.frame, j , with identical dimensions:以及具有相同维度的逻辑 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

I want to remove values from p for which the equivalent value in j is FALSE .我想从p删除j等效值为FALSE值。 How can I do this?我怎样才能做到这一点?

My desired result is:我想要的结果是:

      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

Here's my data:这是我的数据:

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")

Your answer is你的答案是

p[j == FALSE] <- NA

I am sure though that your question must have been answered in some form some place else in the forum though.我相信你的问题一定已经在论坛的其他地方以某种形式得到了回答。

Cheers干杯

或者你可以这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有条件地将一个 data.frame 中的匹配值替换为另一个 data.frame 中的值 - Conditionally replace matching values from one data.frame to values in another data.frame 在R中,如何通过另一个data.frame中的值来子集data.frame? - In R, how do I subset a data.frame by values from another data.frame? 使用来自另一个data.frame的查找值更新data.frame中的列-带有子字符串匹配 - Updating a column in data.frame using lookup values from another data.frame - with substring matching 根据来自R?中另一个data.frame的text.values创建关联data.frame。 - Create a correlation data.frame based on text.values from another data.frame in R? 根据另一个data.frame替换data.frame中的某些列值 - replace some column values from a data.frame based on another data.frame 如何在 R 中的 data.frame 中查找匹配值的索引 - How to find indices for matching values in data.frame in R 对于R中的data.frame,根据来自另一个数据帧的值从一个数据帧提取数据 - For data.frame in R, pulling data from one data frame based on values from another data frame 根据2个匹配的列值将值从一个data.frame添加到另一个 - Adding values from one data.frame to another based on 2 matching column values R如何使用另一个data.frame中的值更新data.frame中的列 - R How to update a column in data.frame using values from another data.frame 如何根据R中的向量从data.frame中提取值? - How can I extract values from a data.frame based on a vector in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM