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?
subset
is designed exactly for this:
subset(test, tot.type >= 2 | tot >= 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With