简体   繁体   中英

Combine rows with same id and delete duplicated rows

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.

data:

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

looks like:

   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

desired output:

   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.

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 aaa 2: 2 aba 3: 2 aca 4: 3 aaa 5: 4 aab 6: 5 aaa 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM