svenkatesh
svenkatesh

Reputation: 1182

R: Select data across multiple columns and conditions

I have a dataframe called test that looks like this:

> test
    dx1     dx2    dx3
1   659     658    657
2   653     651    690 
3   249     786    654
4   647     655    656
5   900     654    658
6   800     224    104

I want to keep only the observations that have at least one column that falls in the range 650 - 660, inclusive. In this case, the result should look like:

    dx1     dx2    dx3
1   659     658    657
2   653     651    690 
3   249     786    654
4   647     655    656
5   900     654    658

So far, I've tried using test[test %in% c(650 : 660)], but this returns a list of numbers in test that satisfy the range without maintaining the dataframe structure. How can I apply the range condition to multiple columns in a dataframe?

Upvotes: 1

Views: 205

Answers (3)

Zach
Zach

Reputation: 1153

Succinctly:

test <- test[apply(test, 1, function(x) any(x >= 650 & x <= 660)), ]

Upvotes: 1

Barker
Barker

Reputation: 2094

You can use apply and any to find the rows of interest and then subset your original.

goodvals <- apply(test <= 660 & test >= 650, 1, any)
test[goodvals, ]

Upvotes: 1

ira
ira

Reputation: 2644

One way to do this is:

# set up your dataset
dx1 <- c(659, 653, 249, 647, 900, 800)
dx2 <- c(658, 651, 786, 655, 654, 224)
dx3 <- c(657, 690, 654, 656, 658, 104)
# bind the created vectors together
test <- cbind(dx1, dx2, dx3)

# filter based on your conditions    
test[(test[, 1] >= 650 & test[, 1] <= 660) | 
     (test[, 2] >= 650 & test[, 2] <= 660)| 
     (test[, 3] >= 650 & test[, 3] <= 660), ]

Upvotes: 1

Related Questions