简体   繁体   中英

R - Create table,how much of a vector in % is contained in another vector

I have a number of vectors containing a set of numbers.

eg:

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM