Reputation: 11480
After merging some data, I have multiple rows per ID. I ONLY want to keep multiple SAME ID's if the data differs. An NA
value should be considered equal to any colwise data point.
df <- structure(list(id = c(1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L),
v1 = structure(c(1L, 1L, NA, 1L, 1L, 1L, 1L, NA, 1L, 1L), .Label = "a", class = "factor"),
v2 = structure(c(1L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, NA, 1L), .Label = c("a",
"b", "c"), class = "factor"), v3 = structure(c(1L, 1L, 1L,
1L, 1L, 1L, NA, 2L, 2L, 1L), .Label = c("a", "b"), class = "factor")), .Names = c("id",
"v1", "v2", "v3"), row.names = c(NA, -10L), class = "data.frame")
id v1 v2 v3
1 a a a
2 a b a
2 <NA> b a
2 a c a
3 a a a
3 a a a
4 a a <NA>
4 <NA> a b
4 a <NA> b
5 a a a
id v1 v2 v3
1 a a a
2 a b a
2 a c a
3 a a a
4 a a b
5 a a a
Happy if there exists a data.table
solution.
Upvotes: 1
Views: 873
Reputation: 440
First replace all NA with a respective column value , then find unique values
library(data.table)
dt<-as.data.table(df)
for (j in seq_len(ncol(dt)))
set(dt,which(is.na(dt[[j]])),j,dt[[j]][1]) #please feel to change dt[[j]][1] to na.omit(dt[[j]])[1] . It is a tradeoff between performance and perfection
unique(dt)
id v1 v2 v3
1: 1 a a a
2: 2 a b a
3: 2 a c a
4: 3 a a a
5: 4 a a a
6: 4 a a b
7: 5 a a a
Upvotes: 1
Reputation: 83215
A possible solution using the data.table
-package:
library(data.table)
setDT(df)[, lapply(.SD, function(x) unique(na.omit(x))), by = id]
which gives:
id v1 v2 v3 1: 1 a a a 2: 2 a b a 3: 2 a c a 4: 3 a a a 5: 4 a a b 6: 5 a a a
Upvotes: 4