I have a data frame containing 92 variables and 1900 observations. Essentially I have species ID as my variable of interest and relative abundance as the other variables.
Columns 69:92 (23 columns) are quality control variables that range from 0-10. I'd like to filter my data so that each species has at least 4 quality control variables that are >5. The best thing I've come up with until now is not what I want but at least filters the data on the basis that I have at least 1 variable with QC>5 and the sum of the row is larger than 100:
df_QC <- df %>%
filter_at(vars(contains("QC")), any_vars(. >= 5))%>%
rowwise()%>%
mutate(total = sum(c_across(69:92)))
filter(total >99.9)
Is there a way to solve it the way I'd like?
To reiterate, I want to select species if QC> 5 for at least 4 of the QC variables.
My data is a bit large so let's work on a smaller dataset to replicate the problem. iris data but add some quality control variables:
df <-cbind(iris, data.frame(qc1 = sample(0:10, size=150, replace=TRUE),
qc2 = sample(0:10, size=150, replace=TRUE),
qc3 = sample(0:10, size=150, replace=TRUE),
qc4 = sample(0:10, size=150, replace=TRUE),
qc5 = sample(0:10, size=150, replace=TRUE)))
#Then similarly I would do a filtering that is not really what I want:
df_QC <- df %>%
filter_at(vars(contains("QC")), any_vars(. >= 5))%>%
rowwise()%>%
mutate(total = sum(c_across(6:10)))%>%
filter(total >15)
So with this example data, how could I filter out Species with QC>5 for at least 3 QCs?
Thanks in advance for any help!