I've got two binary vectors and I'm trying to find the most efficient way of comparing them based on slightly more than just a standard "are they equal?".
My function is that if I have vector x and vector y I want to find out how many times in vector x do I have a 1 at the same index that vector y has a 0. I also need to when vector y has a 1 + has a 0 where vector x also has a 0. (Note: If I find either of these I can just find the inverse to get the other, I'm just not sure which is easier/more efficient ie. VectorY Score = length(VectorX) - VectorX Score)
Ex:
vector x: 1 1 1 0 0 1 - Score: 2
vector y: 0 1 0 1 0 1 - Score: 4
I know that I could just use a for loop to go through each index, but I'd like something more efficient if possible. I have vector lengths of 100 and I need to do many of these comparisons so speed matters.
I tried to use the sum command, but I can't figure out how to add complex conditionals to it. I can find every spot that matches, but that's not enough to solve this.
Ex:
sum(vectorX == vectorY)
Sample:
> vx
[1] 1 1 1 0 0 1
> vy
[1] 0 1 0 1 0 1
You said: "how many times in vector x do I have a 1 at the same index that vector y has a 0"
> vx==1 & vy==0 # constructs this vector:
[1] TRUE FALSE TRUE FALSE FALSE FALSE
> sum(vx==1 & vy==0) # its sum is the answer (TRUE=1, FALSE=0)
[1] 2
You also said: "when vector y has a 1 + has a 0 where vector x also has a 0" which I don't understand but you can clarify that and probably work it out yourself given the answer I've just given you.
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.