I have a dataframe DF, and I want to filter it based on condition from several selected columns.
For instance, I want to filter rows in DF that fulfil the condition that this row contains any values that are smaller than 0.03 in column PCS_AB, PCS_AD, PCS_BD.
DF <- cbind.data.frame(A = c(100, 10, 13),
B = c(33, 44, 12),
D = c(110, 21, 22),
PCS_AB = c(0.03, 0.001, 0.3),
PCS_AD = c(0.01, 0.2, 0.33),
PCS_BD = c(0.99, 1.0, 0.45))
I can achieve it by the following code:
DF_filter <- DF %>%
filter(PCS_AB < 0.03 | PCS_AD < 0.03 | PCS_BD < 0.03)
But I want something simpler like the pseudo code as following:
DF2 <- DF %>%
filter(any(starts_with("PCS")) < 0.03)
Is it possible with dplyr? Thanks.