I have the following data sets that I want to compare for similarities in the elements they contain using a looping strategy that allows all possible combinations (i.e., "setA, setB, setC, setD"; "setA, setB, setC"; "setA, setB", "setB, setC, setD"; "setC, setD"; "setB,setD" etc
Data sets:
setA <- c("dog", "cat", "cow", "sheep", "dunkey")
setB <- c("fox", "cat", "cow", "snake")
setC <- c("dog", "cat", "cow", "sheep", "dunkey", "fox", "python")
setD <- c("dog", "cat", "lion", "sheep", "elephant", "fox")
Not sure how to code this in R, but here's my attempt which did not produce expected results:
similar <- function(...){
Reduce(intersect, list(...))
}
allSets <- list(setA, setB, setC, setD)
for(i in 1:length(allSets)){
similar(allSets[[i]])
similar(allSets[i-1])
similar(allSets[i-2])
similar(allSets[i-3])
}
Can anyone help pls?