Remove least complete duplicate rows in R or SQL

Viewed 827

I have a dataset like this:

id_1 <- c(1, 1, 1)
id_2 <- c(2, NA, NA)
day <- c("Mon", "Mon", "Mon")
month <- c("May", NA, "May")
year <- c("2017", NA, NA)

df <- cbind(id_1, id_2, day, month, year)

These rows are duplicate observations in my data. I would like to keep only the most complete row (i.e. row 1). My real data has 15 columns, so using

duplicated(df[, <some combination of columns>])

seems complicated. Is there a function for this? Or some simple answer I'm overlooking? Answers in R are preferred, but SQL is also a possibility. Thank you in advance!

EDIT: id_1 and id_2 are both identifiers for the observation. id_1 should definitely be unique in this data, but it's ok for id_2 to be NA or repeated for some rows. Eventually I will merge this data table with another data table using id_2. That's why I would like to eliminate rows that repeat information already captured by a row which includes id_2.

3 Answers
Related