Search for unusual elements in several vectors

I am trying to find elements that are not common to multiple vectors. That is, I want to know exactly the elements (and not just their position, etc.), which are not divided by all vectors.

The best implementation I could use uses a nested loop, which, as I understand it, is perhaps the least efficient, especially because the execution is still running when I write this. Here is what I came up with. (each * .id is a vector of the identifier of the Supreme Court case, 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) 

Should there be a more efficient way to do this?

+6
vector search r
source share
1 answer

Trying to find cases where all the judges of the Supreme Court did not participate? Do not think that you have a small data set that you can add?

Thought: move the vectors on top of each other so that you have a data set, for example data.frame ("justice", "case"). Then use the hasley reshape package (use the cast function) to summarize the number of judges in each case. Any case with less than the total number of judges will be a “bad case”.

+3
source share

All Articles