有没有一个通用的函数可以在R中制作块矩阵,其中块的大小不同但每个块的内容相同?...像这样的东西...
#block size
bb <- c(2,3,4,2)
dn <- rep(LETTERS[1:4], times = bb)
#block contents based on 1:16 sequence
matrix(c( 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4,
1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4,
5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8,
5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8,
5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8,
9, 9,10,10,10,11,11,11,11,12,12,
9, 9,10,10,10,11,11,11,11,12,12,
9, 9,10,10,10,11,11,11,11,12,12,
9, 9,10,10,10,11,11,11,11,12,12,
13,13,14,14,14,15,15,15,15,16,16,
13,13,14,14,14,15,15,15,15,16,16),
byrow = TRUE, nrow = sum(bb), ncol = sum(bb), dimnames = list(dn, dn))
# A A B B B C C C C D D
# A 1 1 2 2 2 3 3 3 3 4 4
# A 1 1 2 2 2 3 3 3 3 4 4
# B 5 5 6 6 6 7 7 7 7 8 8
# B 5 5 6 6 6 7 7 7 7 8 8
# B 5 5 6 6 6 7 7 7 7 8 8
# C 9 9 10 10 10 11 11 11 11 12 12
# C 9 9 10 10 10 11 11 11 11 12 12
# C 9 9 10 10 10 11 11 11 11 12 12
# C 9 9 10 10 10 11 11 11 11 12 12
# D 13 13 14 14 14 15 15 15 15 16 16
# D 13 13 14 14 14 15 15 15 15 16 16
...但是没有在matrix
函数中重复设置
matrix(rep(x, rep_len(b, length(x))), ncol = sum(b), byrow = TRUE)[rep(seq_along(b), b), ]
。 - alexis_laz