Home > Blockchain >  Filling matrices with NA to meet desired dimensions
Filling matrices with NA to meet desired dimensions

Time:04-17

I have a list of matrices that I've created. The matrices in the list have different dimensions, and I would like to fill the matrices that don't have a 3x3 dimension with NAs.

I have included my expected outcome below. I would like to include this in a if statement, where if the matrix in the list doesn't have a 3x3 dimension I would like to added empty columns/rows to those matrices and fill them with an NA. Is there an efficient way of doing this in base r?

# Created Matrices
m1 <- matrix(1:9, 3,3)
m2 <- matrix(1:4, 2,2)
m3 <- matrix(1:3, 3, 1)

# Matrices into a list
l1 <- list(m1, m2, m3)
l1


# Expected Matrices and outputs
m2_new <- matrix(c(1,2,NA,3, 4, rep(NA, 4)), 3,3)
m3_new <- matrix(c(1,2,3,rep(NA, 6)), 3,3)
expected <- list(m1, m2_new, m3_new)

CodePudding user response:

One option would be to create a NA matrix and replace the values with the 'x' based on the row/col index

dummy <- matrix(ncol = 3, nrow = 3)
l2 <- lapply(l1, function(x) replace(dummy, cbind(c(row(x)), c(col(x))), x))

-checking

> all.equal(l2, expected)
[1] TRUE

CodePudding user response:

You can replace parts of a matrix with matrix indexing.

mat <- array(dim = c(3, 3))
lapply(l1, function(x) `[<-`(mat, 1:nrow(x), 1:ncol(x), x))

# [[1]]
#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9
# 
# [[2]]
#      [,1] [,2] [,3]
# [1,]    1    3   NA
# [2,]    2    4   NA
# [3,]   NA   NA   NA
# 
# [[3]]
#      [,1] [,2] [,3]
# [1,]    1   NA   NA
# [2,]    2   NA   NA
# [3,]    3   NA   NA

CodePudding user response:

Update see comment by Darren Tsai:

n <- 3
l2 <- lapply(l1, function(x) rbind(x, matrix(ncol = ncol(x),                                       nrow = n - nrow(x))))
x  <- sapply(l2, `length<-`, max(lengths(l2)))
list(m1 = matrix(x[,1],3), m2 = matrix(x[,2],3), m3 = matrix(x[,3],3))
$m1
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$m2
     [,1] [,2] [,3]
[1,]    1    3   NA
[2,]    2    4   NA
[3,]   NA   NA   NA

$m3
     [,1] [,2] [,3]
[1,]    1   NA   NA
[2,]    2   NA   NA
[3,]    3   NA   NA

First answer: not correct output: Here is another approach:

x <- t(sapply(l1, `length<-`, max(lengths(l1))))

l2 <- list(x[,1:3], x[,4:6], x[,7:9])
l2
[[1]]
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    2    3
[3,]    1    2    3

[[2]]
     [,1] [,2] [,3]
[1,]    2    2    2
[2,]    4   NA   NA
[3,]   NA   NA   NA

[[3]]
     [,1] [,2] [,3]
[1,]    3    3    3
[2,]   NA   NA   NA
[3,]   NA   NA   NA

CodePudding user response:

I think there are better solutions but mine will handle a 1x1 matrix as well, which is really just a vector.

You can use the function I've made here resize_matrix in your code however you'd like. It is pretty verbose, but I thinks it's easy to understand exactly what it's doing under the hood. Note: the function is meant to be used in an lapply() call.

The input:

m1 <- matrix(1:9, 3,3)
m2 <- matrix(1:4, 2,2)
m3 <- matrix(1:3, 3, 1)
m4 <- matrix(1:3, 1, 3)
m5 <- matrix(1, 1, 1)

# Matrices into a list
l1 <- list(m1, m2, m3, m4, m5)
l1
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
#> 
#> [[2]]
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    2    4
#> 
#> [[3]]
#>      [,1]
#> [1,]    1
#> [2,]    2
#> [3,]    3
#> 
#> [[4]]
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> 
#> [[5]]
#>      [,1]
#> [1,]    1

The function:

resize_matrix <- function(mat, desired_rows = 3, desired_columns = 3){
  needed_cols <- desired_columns - dim(mat)[2]; needed_cols
  needed_rows <- desired_rows - dim(mat)[1]; needed_rows
  if (dim(mat)[1] == 1 & dim(mat)[2] == 1){
    # we're give a matrix with a single value, expand correctly
    final_mat <- matrix(NA, nrow = desired_rows, ncol = desired_columns)
    final_mat[1,1] <- mat
  } else if (needed_cols > 0 & needed_rows > 0){
    # we need to add both rows and columns
    col_res <- rep(NA, needed_rows)
    row_res <- rep(NA, needed_cols)
    mat_temp1 <- rbind(mat, col_res)
    final_mat <- unname(cbind(mat_temp1, row_res))
  } else if (needed_cols > 0 & needed_rows == 0) {
    # we need to add only columns
    row_res <- matrix(rep(NA, needed_cols), 
                      ncol = needed_cols, nrow = desired_rows)
    final_mat <- unname(cbind(mat, row_res))
    
  } else if (needed_cols == 0 & needed_rows > 0) {
    # we need to add only rows
    col_res <- matrix(rep(NA, needed_rows), 
                      ncol = desired_columns, nrow = needed_rows)
    final_mat <- unname(rbind(mat, col_res))
  } else {
    # we don't need to add anything, return the matrix
    final_mat <- mat
  }
 
  return(final_mat)
}

The output:

lapply(l1, FUN = resize_matrix)
#> [[1]]
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]    1    3   NA
#> [2,]    2    4   NA
#> [3,]   NA   NA   NA
#> 
#> [[3]]
#>      [,1] [,2] [,3]
#> [1,]    1   NA   NA
#> [2,]    2   NA   NA
#> [3,]    3   NA   NA
#> 
#> [[4]]
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA
#> 
#> [[5]]
#>      [,1] [,2] [,3]
#> [1,]    1   NA   NA
#> [2,]   NA   NA   NA
#> [3,]   NA   NA   NA

Created on 2022-04-16 by the reprex package (v2.0.1)

  • Related