Home > Software engineering >  Delete duplicates across rows data.table
Delete duplicates across rows data.table

Time:02-03

After a lot of operations. I have:

dput(result_wide)
structure(list(Node = c("Sante", "Federica", "Filippo", "Francesco", 
"Giovanni", "Marina", "Mario", "Teresa", "Tommaso", "Alex"), 
    `1` = c("Mario", "Mario", "Mario", "Giovanni", "Teresa", 
    "Mario", "Giovanni", "Giovanni", "Mario", "0"), `2` = c("0", 
    "0", "0", "0", "0", "0", "Filippo", "0", "0", "0"), `3` = c("0", 
    "0", "0", "Mario", "Francesco", "0", "Giovanni", "Mario", 
    "0", "0"), `4` = c("0", "0", "0", "0", "Teresa", "0", "Teresa", 
    "Giovanni", "0", "0"), `5` = c("0", "0", "0", "0", "Mario", 
    "0", "Teresa", "Mario", "0", "0"), `6` = c("0", "0", "0", 
    "0", "Mario", "0", "Tommaso", "0", "0", "0"), `7` = c("0", 
    "0", "0", "0", "0", "0", "Marina", "0", "0", "0"), `8` = c("0", 
    "0", "0", "0", "0", "0", "Federica", "0", "0", "0"), `9` = c("0", 
    "0", "0", "0", "0", "0", "Sante", "0", "0", "0"), `10` = c("0", 
    "0", "0", "0", "0", "0", "Francesco", "0", "0", "0")), row.names = c(NA, 
-10L), .internal.selfref = <pointer: 0x562191bd8c70>, class = c("data.table", 
"data.frame"))

This is very close to what I want. Except for the duplicates across rows.

How can I replace with missing values those duplicates across rows?

CodePudding user response:

as.data.table(t(apply(result_wide, 1,\(x) replace(x, duplicated(x), NA))))

  • Related