0

I have a matrix, R of the following form.

       0 0.44 0.77 0.88 0.99
0   1.00 0.75 0.50 0.25    0
0.5 0.75 0.75 0.50 0.25    0
0.6 0.50 0.50 0.25 0.25    0
0.8 0.00 0.00 0.00 0.00    0

here,

colnames(R)
[1] "0"    "0.44" "0.77" "0.88" "0.99"

and

rownames(R)
[1] "0"   "0.5" "0.6" "0.8"

I would like to create different submatrixes based on different combinations of the colnames and rownames of R. For example, a matrix like

       0 0.44
0   1.00 0.75  
0.5 0.75 0.75   
0.6 0.50 0.50

OR

       0 0.44 0.77 0.88 
0   1.00 0.75 0.50 0.25
0.5 0.75 0.75 0.50 0.25 

Thanks.

aynber
  • 22,380
  • 8
  • 50
  • 63
vip123
  • 1
  • 1

1 Answers1

0

You can try the code below

cs <- combn(ncol(R),2,FUN = function(v) v[1]:v[2],simplify = F)
rs <- combn(nrow(R),2,FUN = function(v) v[1]:v[2],simplify = F)
lst <- Map(function(p) Map(function(q) R[p,q],cs),rs)

such that

> head(lst,1)
[[1]]
[[1]][[1]]
       0 0.44
0   1.00 0.75
0.5 0.75 0.75

[[1]][[2]]
       0 0.44 0.77
0   1.00 0.75  0.5
0.5 0.75 0.75  0.5

[[1]][[3]]
       0 0.44 0.77 0.88
0   1.00 0.75  0.5 0.25
0.5 0.75 0.75  0.5 0.25

[[1]][[4]]
       0 0.44 0.77 0.88 0.99
0   1.00 0.75  0.5 0.25    0
0.5 0.75 0.75  0.5 0.25    0

[[1]][[5]]
    0.44 0.77
0   0.75  0.5
0.5 0.75  0.5

[[1]][[6]]
    0.44 0.77 0.88
0   0.75  0.5 0.25
0.5 0.75  0.5 0.25

[[1]][[7]]
    0.44 0.77 0.88 0.99
0   0.75  0.5 0.25    0
0.5 0.75  0.5 0.25    0

[[1]][[8]]
    0.77 0.88
0    0.5 0.25
0.5  0.5 0.25

[[1]][[9]]
    0.77 0.88 0.99
0    0.5 0.25    0
0.5  0.5 0.25    0

[[1]][[10]]
    0.88 0.99
0   0.25    0
0.5 0.25    0

DATA

R <- structure(c(1, 0.75, 0.5, 0, 0.75, 0.75, 0.5, 0, 0.5, 0.5, 0.25, 
0, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0), .Dim = 4:5, .Dimnames = list(
    c("0", "0.5", "0.6", "0.8"), c("0", "0.44", "0.77", "0.88", 
    "0.99")))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81