I have two vectors as an output and they have different lengths. Now, I want to compare each element of one vector with each element of the other vector.
Suppose these are my two vectors:
r1 <- c(1.5,3.5,4.5,5.5,8.5,11.5,12.5,17.5)
min <- c(3,6,11)
And my compare command looks like this:
m1 <- which(abs(outer(min, r1, `-`)) <= 0.5, arr.ind = TRUE)
cbind.data.frame(min = min[m1[,1]], r1 = r1[m1[,2]])
It works and gives me this output:
min r1
1 3 3.5
2 6 5.5
3 11 11.5
What I am asking for now is, how to do the same just by using a for loop (probably with if else statement)? I already came up with this solution, but I don't know how to integrate the tolerance (+/- 0.5)...
z<-rep(0,length(r1))
for(i in 1:length(r1))
{
for(j in 1:length(bottoms[[1]]))
{
if(x[j]>y[i])
z[i]=z[i]+1
}
}
Any ideas?
Aucun commentaire:
Enregistrer un commentaire