'remove repeated records only for some rows with dplyr

I'd like to remove records repeated twice or more times (based on variables: ID, start, drug), but only for some rows. What am I writing wrong?

data <- data %>%
          case_when(ID %in% c(282,464,474)) %>%
          distinct(ID, start, drug, .keep_all = TRUE)


Solution 1:[1]

You could subset data, remove duplicates and join again.

data <- data %>%
          filter(ID %in% c(282,464,474)) %>%
          distinct(ID, start, drug, .keep_all = TRUE) %>%
bind_rows(data %>% 
    filter(!(ID %in% c(282,464,474))))

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Josep Pueyo