I have two sequences. These are times in seconds. I want to know which values in sequence b occur within 10 seconds of any value in sequence a.
seqa = c(4.53333333333333, 7.43333333333334, 9.03333333333333, 20.6166666666667,
20.6333333333333, 42.5666666666667, 48.3166666666667, 48.8, 49.75,
55.1, 56.7833333333333, 59.3833333333333, 110.15, 113.95, 114.6)
seqb = c(18.3833333333333, 18.3833333333333, 63.8833333333333, 72.3166666666667,
76.7166666666667, 85.2166666666667, 91.25, 91.3666666666667,
96.2833333333333)
I did this using two cycles for. Passage of each element seqband testing, if it occurs at a point in time, exceeding each value seqa, but within 10 seconds.
matX <- matrix(nrow=length(seqa), ncol=length(seqb))
for(j in seq_along(seqb)){
for(i in seq_along(seqa)){
test1 <- seqb[j]>=seqa[i]
test2 <- seqb[j]<=seqa[i]+10
matX[i,j] <- sum(test1 + test2)
}
}
matX
I save the results in a matrix. You can see the values of 2 in columns 1, 2, and 3.
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 1 1 1 1 1 1 1 1
[2,] 1 1 1 1 1 1 1 1 1
[3,] 2 2 1 1 1 1 1 1 1
[4,] 1 1 1 1 1 1 1 1 1
[5,] 1 1 1 1 1 1 1 1 1
[6,] 1 1 1 1 1 1 1 1 1
[7,] 1 1 1 1 1 1 1 1 1
[8,] 1 1 1 1 1 1 1 1 1
[9,] 1 1 1 1 1 1 1 1 1
[10,] 1 1 2 1 1 1 1 1 1
[11,] 1 1 2 1 1 1 1 1 1
[12,] 1 1 2 1 1 1 1 1 1
[13,] 1 1 1 1 1 1 1 1 1
[14,] 1 1 1 1 1 1 1 1 1
[15,] 1 1 1 1 1 1 1 1 1
out <- apply(matX, 2, function(x) any(x>=2))
seqb[out]
# [1] 18.38333 18.38333 63.88333
, 10 seqa. ( 10 9.03333, 63.8333 10 seqa (55.1, 56.78333, 59.38333).
, 2000 . .