digivet-consortium/movenet

create_local_sp_matrix & create_move_sp_matrix ordering doesn't work if non-numeric

Closed this issue · 1 comments

The below ordering of matrices doesn't work for non-numeric holding ids - consider reworking.

movement_spread_matrix <-
    movement_spread_matrix[order(as.numeric(rownames(movement_spread_matrix))),
                           order(as.numeric(colnames(movement_spread_matrix)))]

(Simply taking out as.numeric() doesn't order numeric ids correctly)

Solved with tryCatch:

movement_spread_matrix <-
  tryCatch(
    {movement_spread_matrix[order(as.numeric(rownames(movement_spread_matrix))),
                            order(as.numeric(colnames(movement_spread_matrix)))]},
    warning = function(cond){
      movement_spread_matrix[order(rownames(movement_spread_matrix)),
                             order(colnames(movement_spread_matrix))]
    })

@mdenwood do you reckon it is safe enough to assume the matrix ordering on as.numeric(rownames) and as.numeric(colnames) will only give a warning if as.numeric() introduces NAs for non-numeric rownames/colnames?

  • Never mind, will replace with an if-else solution based on if(is.na(as.numeric(rownames(matrix))))