I want to remove all duplicates across multiple vectors, leaving none. For example, for these vectors:
a <- c("dog", "fish", "cow")
b <- c("dog", "horse", "mouse")
c <- c("cat", "sheep", "mouse")
the expected result would be:
a <- c("fish", "cow")
b <- c("horse")
c <- c("cat", "sheep")
Is there a way to achieve this without concatenating the vectors and splitting them again?
You could perhaps do:
vec <- c(a, b, c)
sapply(list(a, b, c), function(x) x[!x %in% vec[duplicated(vec)]])
[[1]]
[1] "fish" "cow"
[[2]]
[1] "horse"
[[3]]
[1] "cat" "sheep"
If you need individual variables in the global environment, with the addition of lst() from tibble:
vec <- c(a, b, c)
l <- sapply(lst(a, b, c), function(x) x[!x %in% vec[duplicated(vec)]])
list2env(l, envir = .GlobalEnv)
Given data in a list, e.g., lst <- list(a = a, b = b, c = c), you can try
> unstack(subset(stack(lst), ave(seq_along(values), values, FUN = length) == 1))
$a
[1] "fish" "cow"
$b
[1] "horse"
$c
[1] "cat" "sheep"
> lapply(seq_along(lst), \(k) setdiff(lst[[k]], unlist(lst[-k])))
[[1]]
[1] "fish" "cow"
[[2]]
[1] "horse"
[[3]]
[1] "cat" "sheep"
> v <- names(which(table(unlist(lst)) == 1))
> lapply(lst, intersect, v)
$a
[1] "fish" "cow"
$b
[1] "horse"
$c
[1] "cat" "sheep"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With