Reputation: 1
I have a number of vectors containing a set of numbers.
e.g.:
v1 <- c(15,12,50,2007,1828)
v2 <- c(50,2007,11,8)
in the next step i Want to see how much of vector 2 in Percent is contained in vector 1
sim <- length(which(v2%in%v1 ==T)) / length(v2)
I created a for loop for that ,checking v1 versus v2,v3,v4.... and then v2 versus v1,v3,4... If the sim value was bigger than 10% i wanted to enter that in a table.
Because of the number of vectors ~ 1000. The for loop is taking way to long. Are there any alternatives?
Upvotes: 0
Views: 35
Reputation: 725
You should use the set operator intersect
First, calculate the intersection of the two vectors
shared <- intersect(v1,v2)
Next, calculate the percent of shared elements in v2
sim <- length(shared)/length(v2)
If you type ?intersect
in your R command line, you will see there are also other helpfull setoperations like union
and setdiff
Upvotes: 1