[英]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.