Jonathan1234
Jonathan1234

Reputation: 499

Rearrange rows of Matrix in R

I created the following matrix, in R


P = as.matrix(expand.grid(0:1, 0:1, 0:1, 0:1))
P = P[-1,]
        Var1 Var2 Var3 Var4
 [1,]    1    0    0    0
 [2,]    0    1    0    0
 [3,]    1    1    0    0
 [4,]    0    0    1    0
 [5,]    1    0    1    0
 [6,]    0    1    1    0
 [7,]    1    1    1    0
 [8,]    0    0    0    1
 [9,]    1    0    0    1
[10,]    0    1    0    1
[11,]    1    1    0    1
[12,]    0    0    1    1
[13,]    1    0    1    1
[14,]    0    1    1    1
[15,]    1    1    1    1

Is there a way to arrange the rows of P and obtain the following ??

P = matrix(c(1,1,1,1,0,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,0,1,1,0,0,0,1,0,0,1,0,0,0),10,4,byrow=TRUE)
     [,1] [,2] [,3] [,4]
 [1,]    1    1    1    1
 [2,]    0    1    1    1
 [3,]    0    0    1    1
 [4,]    0    0    0    1
 [5,]    1    1    1    0
 [6,]    0    1    1    0
 [7,]    0    0    1    0
 [8,]    1    1    0    0
 [9,]    0    1    0    0
[10,]    1    0    0    0

In a generic way? i.e. if I increase the colums of P as as.matrix(expand.grid(0:1, 0:1, 0:1, 0:1, 0:1, 0:1)) I would like to have an equivalent rearrangement.

Upvotes: 1

Views: 59

Answers (2)

Rui Barradas
Rui Barradas

Reputation: 76402

Here is a function that creates the matrix in the question and is extensible to any number of columns.

makeMat <- function(n){
  f <- function(n){
    p <- diag(n)
    p[upper.tri(p)] <- 1
    p
  }
  P <- lapply(rev(seq.int(n)), f)
  P[-1] <- lapply(seq_along(P)[-1], function(i, n){
    Q <- matrix(0, nrow = n - i + 1, ncol = i - 1)
    cbind(P[[i]], Q)
  }, n = n)
  do.call(rbind, P)
}

makeMat(4)
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    1    1
# [2,]    0    1    1    1
# [3,]    0    0    1    1
# [4,]    0    0    0    1
# [5,]    1    1    1    0
# [6,]    0    1    1    0
# [7,]    0    0    1    0
# [8,]    1    1    0    0
# [9,]    0    1    0    0
#[10,]    1    0    0    0

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

You can try creating the matrix you want directly by using something like this:

fun <- function(nc = 4) {
  out <- lapply(rev(seq.int(nc)), function(x) {
    a <- matrix(1L, ncol = x, nrow = x)
    a[lower.tri(a)] <- 0L
    if (x == nc) {
      a
    } else {
      b <- matrix(0L, ncol = nc - x, nrow = nrow(a))
      cbind(a, b)
    }
  })
  do.call(rbind, out)
}

fun(4)
#       [,1] [,2] [,3] [,4]
#  [1,]    1    1    1    1
#  [2,]    0    1    1    1
#  [3,]    0    0    1    1
#  [4,]    0    0    0    1
#  [5,]    1    1    1    0
#  [6,]    0    1    1    0
#  [7,]    0    0    1    0
#  [8,]    1    1    0    0
#  [9,]    0    1    0    0
# [10,]    1    0    0    0

Upvotes: 3

Related Questions