Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling setdiff() on multiple vectors

Tags:

r

How can I use setdiff() in R to get the elements that are in one vector but not in the others My example is as follows:

dat1 <- c("osa", "bli", "usd", "mnl")
dat2 <- c("mnu", "erd", "usd", "mnl")
dat3 <- c("ssu", "erd", "usd", "mnl")

The following code only returns what is diffrent in dat1 compared to dat2 and dat3:

diffs <- Reduce(setdiff, 
        list(A = dat1,
             B = dat2,
             C = dat3
)

How can I modify this code to be able to get all the elements that are uniquely present in on vector compared to the other? Thanks

like image 910
user27976 Avatar asked Mar 27 '14 10:03

user27976


3 Answers

another solution using setdiff :

myl <- list(A = dat1,
            B = dat2,
            C = dat3)
lapply(1:length(myl), function(n) setdiff(myl[[n]], unlist(myl[-n])))

[[1]]
[1] "osa" "bli"

[[2]]
[1] "mnu"

[[3]]
[1] "ssu"
like image 123
SeDur Avatar answered Nov 12 '22 21:11

SeDur


a second possibility :

f <- function (...) 
{
  aux <- list(...)
  ind <- rep(1:length(aux), sapply(aux, length))
  x <- unlist(aux)
  boo <- !(duplicated(x) | duplicated(x, fromLast = T))
  split(x[boo], ind[boo])
}
f(dat1, dat2, dat3)
$`1`
[1] "osa" "bli"

$`2`
[1] "mnu"

$`3`
[1] "ssu"
like image 44
droopy Avatar answered Nov 12 '22 21:11

droopy


Try this:

all.dat    <- list(dat1, dat2, dat3)
from.dat   <- rep(seq_along(all.dat), sapply(all.dat, length))
in.dat     <- split(from.dat, unlist(all.dat))
in.one.dat <- in.dat[sapply(in.dat, length) == 1]
in.one.dat
# $bli
# [1] 1

# $mnu
# [1] 2

# $osa
# [1] 1

# $ssu
# [1] 3

which tells you what items are found in only one of the dat objects, and which one. If you only care for the names, then finish with: names(in.one.dat).

like image 27
flodel Avatar answered Nov 12 '22 21:11

flodel