简体   繁体   English

选择两个值符合特定阈值的行

[英]Select rows where two values conform to a certain threshold

I am using R and I need to select the rows where any value is > 0.7 and any other value < 0.4. 我正在使用R,我需要选择任何值> 0.7且任何其他值<0.4的行。 Two values should meet this requirement. 两个值应满足此要求。 If there is another value in the row that does not apply to this I still want to take this row. 如果行中有另一个不适用于此的值,我仍然希望使用该行。

Df: Df:

    A   B   C   D   E
1   NA  0.5 0.6 NA  NA
2   NA  0.2 0.8 NA  NA
3   NA  0.2 NA  0.6 NA
4   NA  0.6 0.1 0.8 NA

The output should look like this: 输出应如下所示:

    A   B   C   D   E
2   NA  0.2 0.8 NA  NA
4   NA  0.6 0.1 0.8 NA

Try (thanks to @akrun for the edit suggestion) 试试(感谢@akrun提供编辑建议)

df[!!rowSums(df > .7, na.rm = TRUE) & !!rowSums(df < 0.4, na.rm = TRUE), ]
##    A   B   C   D  E
## 2 NA 0.2 0.8  NA NA
## 4 NA 0.6 0.1 0.8 NA

Or (worse option) 或(更差的选项)

df[apply(df, 1, function(x) any(x > .7, na.rm = TRUE) & any(x < .4, na.rm = TRUE)), ]
#    A   B   C   D  E
# 2 NA 0.2 0.8  NA NA
# 4 NA 0.6 0.1 0.8 NA

You could also try: 您也可以尝试:

 Df[intersect(which(Df >0.7, arr.ind=TRUE)[,1] , which(Df <0.4,arr.ind=TRUE)[,1]),]
 #     A   B   C   D  E
 #2 NA 0.2 0.8  NA NA
 #4 NA 0.6 0.1 0.8 NA

data 数据

 Df <- structure(list(A = c(NA, NA, NA, NA), B = c(0.5, 0.2, 0.2, 0.6
 ), C = c(0.6, 0.8, NA, 0.1), D = c(NA, NA, 0.6, 0.8), E = c(NA, 
 NA, NA, NA)), .Names = c("A", "B", "C", "D", "E"), class = "data.frame", row.names = c("1", 
 "2", "3", "4"))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM