'Filter only if another variable meets a condition

I would like to conditionally filter a data frame. E.g. with diamonds dataset, if cut == 'Ideal', then filter price. Example sudo code:

library(tidyverse)
diamonds |> 
  filter(if cut == 'Ideal' then price > 5000 else do nothing)

Is this possible with dplyr's filter()?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source