This is probably a simple question for those experienced in R, but it is something that I (a novice) am struggling with...
I have two examples of vectors that are common to the problem I am trying to solve, A
and B
:
A <- c(1,3,NA,3,NA,4,NA,1,7,NA,2,NA,9,9,10)
B <- c(1,3,NA,NA,NA,NA,NA,NA,NA,NA,2,NA,9)
#and three scalars
R <- 4
t <- 5
N <- 3
There is a fourth scalar, n
, where 0<=n<=N
. In general, N <= R
.
I want to find the n
closest non-NA
values to t
such that they fall within a radius R
centered on t
. I.e., the search radius, R
comprises of R+1
values. For example A, the search radius sequence is (3,NA,3,NA,4,NA,1)
, where t=NA
, the middle value in the search radius sequence.
The expected answer can be one of two results for A:
answerA1 <- c(3,4,1)
OR
answerA2 <- c(3,4,3)
The expected answer for B:
answerB <- c(1,3)
How would I accomplish this task in the most time- and space-efficient manner? One liners, loops, etc. are welcome. If I have to choose a preference, it is for speed!
Thanks in advance!
Note:
For this case, I understand that the third closest non-NA
value may involve choosing a preference for the third value to fall on either the right or left of t
(as shown by the two possible answers above). I do not have a preference for whether this values falls to the left or the right of t
but, if there is a way to leave it to random chance, (whether the third value falls to the right or the left) that would be ideal (but, again, it is not a requirement).