Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding uncommon elements across multiple vectors

Tags:

search

r

vector

I am attempting to find the elements that are not common across multiple vectors. That is, I want to know exactly the elements (not just their position, etc.) that are not shared across all vectors.

The best implementation I could come up with uses a nested-loop, which I realize is probably the least efficient, most notably because the execution is still running as I write this. Here is what I came up with. (each *.id is a vector of Supreme Court case ID's stored as strings).

check.cases<-TRUE

if(check.cases) {
    all.cases<-c(AMKennedy.id,AScalia.id,CThomas.id,DHSouter.id,JGRoberts.id,JPStevens.id,RBGinsburg.id,SAAlito.id,SGBreyer.id)
    bad.cases<-c()
    for(b in all.cases) {
        for(t in all.cases) {
            m<-match(t,b)
            bad<-t[which(is.na(m))]
            bad.cases<-append(bad.cases,bad)
        }
    }
    bad.cases<-unique(bad.cases)
}

print(bad.cases)

There must be a more efficient way of doing this?

like image 355
DrewConway Avatar asked Oct 02 '09 14:10

DrewConway


People also ask

How do you find the common element of multiple vectors?

1 Answer. To find the common elements from multiple vectors, you can use the intersect function from the sets base R package. vectors (of the same mode) containing a sequence of items (conceptually) with no duplicate values. Intersect will discard any duplicated values in the arguments, and they apply as.

How do you find the intersection of two lists in R?

intersect() function in R Language is used to find the intersection of two Objects. This function takes two objects like Vectors, dataframes, etc. as arguments and results in a third object with the common data of both the objects.

What is the opposite of intersect in R?

outersect(): The opposite of R's intersect() functionsetdiff() produces all elements of the first input vector without any matching elements from the second input vector (i.e. is asymmetric).


1 Answers

Trying to find cases where all the Supreme Court justices weren't involved? Don't suppose that you have a small sample dataset that you could add?

A thought: rbind the vectors on top of each other so that you have a dataset like data.frame("justice","case"). Then use hadley's reshape package (use the cast function) to sum the number of justices per case. Any case with less than the total number of justices will be a "bad case".

like image 158
Shane Avatar answered Oct 18 '22 06:10

Shane