Somehow only today did I realise that you can use if statements directly within R’s dplyr library filter function in order to make conditional filter criteria.

For example if you want to filter a fictional dataframe to show only scores below 10, but only if the variable x has the value “filter” you can do:

filter(data, if (x == "filter") {
      my_score < 10
    } else {
      my_score == my_score
    })

A variable always equals itself, so the score == score bit effectively means “don’t apply a filter in this case”. You can of course use other criteria there if you want to apply different filters based on different conditions.