我有一个如下表:
dput(tail(dt[,c("DSALENew","PPEGTNew","ACNew")],5))
structure(list(DSALENew = c(1.2, 1.54, 1.1, 12, 1.1),
PPEGTNew = c(4, 1.2, 2.2, 1.1, 2), ACNew = c(458, 1.2, 1.5,
1.88, 3.2)), .Names = c("DSALENew", "PPEGTNew", "ACNew"), row.names = c(139728L, 139730L, 139731L, 139732L, 139733L), class = "data.frame")
我只想为DSALENew和PPEGTNew列选择值在1到2之间的那些行。我该怎么做?谢谢。
另一种方式:通过mrip使用subset
and%between%
运算符:
`%between%`<-function(x,rng) x>rng[1] & x<rng[2]
subset(x, DSALENew %between% c(1,2) & PPEGTNew %between% c(1,2))
## DSALENew PPEGTNew ACNew
## 139730 1.54 1.2 1.2
但是要小心您想要的东西:>
或>=
如果您有多个变量,并且所有变量只有一个条件,则可以执行以下操作:
## Data
set.seed(85)
x <- as.data.frame(matrix(round(runif(1000, min=1, max=3), 3), ncol=10))
## Condition applied on each column
index <- sapply(1:ncol(x), function(i) x[, i] %between% c(1,2))
## For which row the condition is true for all column
index <- apply(index, 1, all)
x[index, ]
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
## 3 1.573 1.403 1.128 1.333 1.011 1.697 1.407 1.626 1.656 1.237
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句