0

I am dealing with a dataset like this

    Id       Value       Date
    1        250         NA
    1        250         2010-06-21
    2        6           NA
    2        6           2012-08-23
    3        545         NA
    7        3310        NA

My goal is to remove entire rows if there is an NA in Date column and ID is duplicate. The final output should look like:

    Id       Value       Date
    1        250         2010-06-21
    2        6           2012-08-23
    3        545         NA
    7        3310        NA
d.b
  • 32,245
  • 6
  • 36
  • 77

1 Answers1

1
df1[!(is.na(df1$Date) & duplicated(df1$Id) | duplicated(df1$Id, fromLast = TRUE)),]
#  Id Value       Date
#2  1   250 2010-06-21
#4  2     6 2012-08-23
#5  3   545       <NA>
#6  7  3310       <NA>

DATA

df1 = structure(list(Id = c(1L, 1L, 2L, 2L, 3L, 7L), Value = c(250L, 
250L, 6L, 6L, 545L, 3310L), Date = c(NA, "2010-06-21", NA, "2012-08-23", 
NA, NA)), .Names = c("Id", "Value", "Date"), class = "data.frame", row.names = c(NA, 
-6L))
d.b
  • 32,245
  • 6
  • 36
  • 77