Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows based on multiple conditions using OR instead of AND in R

I have a large dataset that I am trying to filter based on the value of 2 separate columns. For each row I have a column showing their total count (tot) and a column showing the total times that type of sample was seen (tot.type).

I want to filter my data based on both (tot) and (tot.type) where (tot) OR (tot.type) must be greater than or equal to 2, for example.

All examples I have found for filtering based on multiple values use "AND" but nothing where you use "OR"...

Example data:
name = c("A","B","C","D","E")
rx = c(1,0,2,1,1)
ry = c(0,1,1,0,0)
rz = c(0,0,2,2,3)
type = c("p","q","r","p","r")
tot = c(1,1,5,3,4)
tot.type = c(2,1,2,2,2)
test = data.frame(name,rx,ry,rz,tot,tot.type)

In this example I would discard row B, and keep the rest.

I have filtered the data into 2 separate data sets based on just one column or the other, and then merged them but can this be done in one line that generates one data set, rather than doing two separate ones and combining them later?

like image 206
Ina.Quest Avatar asked Feb 12 '23 00:02

Ina.Quest


1 Answers

subset is designed exactly for this:

subset(test, tot.type >= 2 | tot >= 2)
like image 127
BrodieG Avatar answered Apr 29 '23 15:04

BrodieG