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?
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.
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.
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).
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".
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