Amin Haque Amin Haque - 9 months ago 32
R Question

Filter in one column then filter occurences of a word

How would I count the number of occurences after I filter a dataset in regards to one column (e.g "Variant" column for "A/T") then subsequently filter in another column for words containing a particular word(e.g "SEQ" column for "G[A]C")?

Ive tried the following but received an error:

length(which(mydata$VARIANT =="A/T") & grep(length("G[A]A", mydata$SEQ)))


Checking in excel, filtering for just 'A/T' reveals 9 then there are 2 containing 'G[A]C'

Answer Source

When using &, we need logical vectors. So, instead of grep, it should be grepl

sum(mydata$VARIANT == "A/T" & grepl("G[A]A", mydata$SEQ, fixed = TRUE))