R-fill data gaps with length "n" or less along the matrix diagonal

I am working with some large matrices with diagonal values ​​like the following.

ontrack <- matrix(c(
         runif(1),NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         runif(1),NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,runif(1),NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,runif(1),runif(1),NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,runif(1),NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
         NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,runif(1)),
         nrow=14, byrow=T
         )

      

I would like to fill in gaps of data of length "n" or less to connect the diagonal segments. For example, using the above matrix and filling in data gaps of 3 or less, I would like to go from this:

diag_indx <- which(!is.na(ontrack), arr.ind=T)

      

which gives

     row col
[1,]   1   1
[2,]   2   1
[3,]   3   3
[4,]   7   5
[5,]   7   6
[6,]   9   8
[7,]  14  13

      

to that

     row col
       1   1
       2   1
 newV  3   2
       3   3
  new  4   4
  new  5   4
  new  6   4
       7   5
       7   6
  new  8   7
       9   8
      14  13

      

For cases such as newV

, the result could be (2.2) or (3.2). My subsequent code uses a matrix diag_indx

, but data gaps can be filled directly in the matrix ontrack

(using any value in order) if it is more efficient.

When trying to work out a solution, I assumed to find the data gaps in the matrix diag_indx

using this sequence length equation

seqle <- function(x, incr=1) { 
  if(!is.integer(x)) x <- as.integer(x) 
  n <- length(x)  
  y <- x[-1L] != x[-n] + incr 
  i <- c(which(y|is.na(y)),n) 
  list(lengths = diff(c(0L,i)),
       values = x[head(c(0L,i)+1L,-1L)]) 
}

      

and then fill in the data gaps with seq()

. I'm just not sure how to effectively do this. Thank you for your help.

+3


source to share


1 answer


After some trial and error, I came up with a (not so pretty) solution that only requires basic R functions.

diagFillSeq <- function(diag_indx, fillgap=1){
  repeat{
    for(cols in 1:2){
      diag_indx <- diag_indx[order(diag_indx[, cols]), ] #Sort by selected column
      repeat{
        diffs <- diff(diag_indx[, cols]) 
        #Find breaks in sequence with differences >1 (diffs==1 are in sequence) and less than or equal to fillgap
        gap_indx <- which(diffs > 1 & diffs <= (fillgap +1)) #need +1 because fencepost error: 3rd & 7th post diffs=4 but fillgap=3)
        if(length(gap_indx) == 0){break}
        insert_indx <- gap_indx[1]
        seq_length <- diffs[gap_indx[1]] - 1  #need -1 because fencepost error
        #Subset diag_indx and insert filling sequence
        diag_indx <- rbind(diag_indx[1:insert_indx, ],
                      cbind(
                        as.integer( seq(from=diag_indx[insert_indx, 1] +1, to=diag_indx[insert_indx+1, 1] -1, length.out=seq_length) ),
                        as.integer( seq(from=diag_indx[insert_indx, 2] +1, to=diag_indx[insert_indx+1, 2] -1, length.out=seq_length) ) 
                      ),
                      diag_indx[(insert_indx+1):nrow(diag_indx), ]) 
      }
    }
    #Recheck first column to see if any new sequence gaps were created
    diffs <- diff(diag_indx[, 1])
    gap_indx <- which(diffs > 1 & diffs <= (fillgap +1))
    if(length(gap_indx) == 0){return(unname(diag_indx))}
  }
}

      



And test for diag_indx

above

whatIwant <- matrix(as.integer(c(1,2,3,3,4,5,6,7,7,8,9,14, 1,1,2,3,4,4,4,5,6,7,8,13)), ncol=2)
whatIwant
#      [,1] [,2]
# [1,]    1    1
# [2,]    2    1
# [3,]    3    2
# [4,]    3    3
# [5,]    4    4
# [6,]    5    4
# [7,]    6    4
# [8,]    7    5
# [9,]    7    6
#[10,]    8    7
#[11,]    9    8
#[12,]   14   13

identical(diagFillSeq(diag_indx, fillgap=3), whatIwant)
#TRUE

      

+1


source







All Articles