按ID删除NAs

时间:2018-03-05 16:31:35

标签: r na

我正在处理像这样的数据集

    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

我的目标是删除整个行,如果 NA列中有DateID重复。最终输出应如下所示:

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

1 个答案:

答案 0 :(得分: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>

数据

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))