I am trying to match values from one vector (1,7) to a matrix (2,21)
The goal here is to match the vector values to the matrix values as there are equal values within both, shown in example e.g.
if(Removed[,1] %like% T1PickProb[2]){
print("yes")
} else{
print("no match")
}
T1PickProb
[output]
0.01754386 0.06140351 0.13157895 0.23684211 0.28070175 0.17543860 0.09649123
Removed
[output]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18]
[1,] 0.01754386 0.01754386 0.01754386 0.01754386 0.01754386 0.01754386 0.06140351 0.06140351 0.06140351 0.06140351 0.06140351 0.1315789 0.1315789 0.1315789 0.13157895 0.2368421 0.2368421 0.23684211
[2,] 0.06140351 0.13157895 0.23684211 0.28070175 0.17543860 0.09649123 0.13157895 0.23684211 0.28070175 0.17543860 0.09649123 0.2368421 0.2807018 0.1754386 0.09649123 0.2807018 0.1754386 0.09649123
[,19] [,20] [,21]
[1,] 0.2807018 0.28070175 0.17543860
[2,] 0.1754386 0.09649123 0.09649123
This should print out "yes" as T1PickProb[2] is 0.06140351 which is in Removed[,1] which has two values 0.01754386 & 0.06140351. However it prints out "no match"
So my question is, is there a way I can match values to specific columns within the matrix? I have tried the following
Removed[,1]
but this returns the following error when running the about if statement:
In if (Removed[, 1] %like% T1PickProb[2]) { :
the condition has length > 1 and only the first element will be used
I have also tried
Removed[1,2]
However this return the first value in the second column. What else can I do?
Just to illustrate:
if(any(removed[,1] %in% T1PickProb[2])){
print("yes")
} else{
print("no match")
}
> "yes"
That is because
> removed[,1] %in% T1PickProb[2]
[1] FALSE TRUE
yields a vector with two elements. To see if any of these two is true, you can use any
:
> any(removed[,1] %in% T1PickProb[2])
[1] TRUE
You can also vectorise this further, e.g.
Using the fact that:
removed %in% T1PickProb
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[21] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
[41] TRUE TRUE
you could do
ifelse(removed %in% T1PickProb, "yes", "no match")
[1] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes"
[12] "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes" "yes"
[23] "no match" "no match" "no match" "no match" "no match" "yes" "yes" "yes" "no match" "no match" "no match"
[34] "yes" "yes" "yes" "no match" "yes" "yes" "yes" "yes" "yes"