Reputation: 2250
I have a table as given below:
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")
I want to select only those rows which has values between 1 and 2 for columns DSALENew and PPEGTNew. How can I do the same? Thanks.
Upvotes: 0
Views: 1933
Reputation: 13856
An other way : with subset
and %between%
operator by mrip :
`%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
But be careful of what you want : >
or >=
If you have several variables and only one condition for all the variable you could do :
## 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
Upvotes: 1
Reputation: 1974
Suppose that dat
is your data frame.
You can use the following check
check <- rowMeans(dat[,1:2] > 1 & dat[,1:2] < 2) == 1
dat[check,]
Upvotes: 1
Reputation: 7130
> library(dplyr)
> filter(df, DSALENew > 1, DSALENew < 2, PPEGTNew > 1, PPEGTNew < 2)
DSALENew PPEGTNew ACNew
1 1.54 1.2 1.2
Upvotes: 2
Reputation: 3525
call that data.frame x
x[x$DSALENew >=1 & x$DSALENew <=2 & x$PPEGTNew >=1 & x$PPEGTNew <=2,]
Upvotes: 2