0

I am trying to write some code to only keep rows in my data frame that:

have the same ID but different Dates.

Below is my current and desired application date.

current

current

desired

desired

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • because that ID only appears once – coding2131413 May 17 '21 at 01:43
  • Images are not the right way to share data/code. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 17 '21 at 04:11

1 Answers1

1

We can group by 'ID', filter the IDs with number of distinct 'Date' are greater than 1

library(dplyr)
df1 %>%
    group_by(ID) %>%
    filter(n_distinct(Date, na.rm = TRUE) > 1) %>%
    ungroup

Or in base R

subset(df1, ID %in% names(which(table(unique(df1[c('ID', 'Date')])$ID) > 1)))
akrun
  • 874,273
  • 37
  • 540
  • 662