如何在R中的多个列上选择具有条件的行

时间:2014-02-27 09:07:39

标签: r

我有一张表格如下:

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之间的行。我怎么能这样做?感谢。

4 个答案:

答案 0 :(得分:2)

调用data.frame x

x[x$DSALENew >=1 & x$DSALENew <=2 & x$PPEGTNew >=1 & x$PPEGTNew <=2,]

答案 1 :(得分:2)

> library(dplyr)
> filter(df, DSALENew > 1, DSALENew < 2, PPEGTNew > 1, PPEGTNew < 2)
  DSALENew PPEGTNew ACNew
1     1.54      1.2   1.2

答案 2 :(得分:1)

假设dat是您的数据框。

您可以使用以下检查

check <- rowMeans(dat[,1:2] > 1 & dat[,1:2] < 2) == 1
dat[check,]

答案 3 :(得分:1)

另一种方式:通过mrip使用subset%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