0

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.

Henrik
  • 65,555
  • 14
  • 143
  • 159
Sumit
  • 2,242
  • 4
  • 25
  • 43
  • Thanks for all the solution. But let say I say 20 such columsn to do the filtering for. How to do the same for 20 columns? Let say colnames is the list of columns to do the filtering for. – Sumit Feb 27 '14 at 09:40

4 Answers4

2

call that data.frame x

x[x$DSALENew >=1 & x$DSALENew <=2 & x$PPEGTNew >=1 & x$PPEGTNew <=2,]
JeremyS
  • 3,497
  • 1
  • 17
  • 19
2
> library(dplyr)
> filter(df, DSALENew > 1, DSALENew < 2, PPEGTNew > 1, PPEGTNew < 2)
  DSALENew PPEGTNew ACNew
1     1.54      1.2   1.2
Nishanth
  • 6,932
  • 5
  • 26
  • 38
1

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,]
Lars Lau Raket
  • 1,905
  • 20
  • 35
1

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
Victorp
  • 13,636
  • 2
  • 51
  • 55