我有一个xtabs表的列表,这些表的行长不同,但列长相同。行名是两个字母等级的组合,例如“ A-B”,并且列名是单个字母等级,例如“一种”。我想使所有矩阵具有相同的维数,然后添加它们。
由于列表中的矩阵具有不同的维度,因此我知道必须先将它们转换为相同的维度,然后才能添加它们。我创建了一个矩阵,其中包含字母等级(36 x 6)的所有可能组合。如何使列表中的所有矩阵都具有与36 x 6矩阵相同的尺寸并确保遵守顺序?
row.order <- c( "Aa", "A", "Baa", "Ba", "B", "Caa")
# all possible combinations
all.crossings <- expand.grid(row.order, row.order, row.order)
all.crossings <-
all.crossings %>%
mutate(ij = paste(Var1, Var2, sep = "-"),
k = Var2,
Count = 0) %>%
select(ij, k, Count)
# use xtabs to transfor into matrix form
all.crossings <- xtabs(Count~ij+k, data = all.crossings)
attributes(all.crossings)$class <- "matrix"
这是我的xtab矩阵列表的样子:
> data_out.2nd.ord
[[1]]
k
ij Aa A Baa Ba B Caa
A-Ba 0 0 0 1 1 0
A-Baa 0 0 2 1 2 0
Aa-A 1 2 0 0 0 0
Aa-Ba 0 0 0 1 0 0
Aa-Baa 0 2 0 1 1 0
B-A 0 0 0 1 0 0
B-B 0 0 1 3 14 5
B-Ba 0 0 1 1 3 3
B-Baa 0 0 2 0 0 1
B-Caa 0 0 1 3 5 7
Ba-A 0 0 0 2 0 0
Ba-B 0 0 2 5 3 2
Ba-Ba 0 0 1 2 5 0
Ba-Baa 0 1 0 1 1 1
Ba-Caa 0 0 1 1 2 3
Baa-A 0 0 0 2 0 0
Baa-Aa 0 1 0 0 0 0
Baa-B 0 0 0 2 1 2
Baa-Ba 0 0 3 2 1 0
Baa-Baa 1 0 3 2 1 0
Baa-Caa 0 0 1 0 1 0
Caa-B 0 0 0 0 6 6
Caa-Ba 0 0 0 0 1 0
Caa-Baa 0 0 1 0 0 0
Caa-Caa 0 0 0 2 5 12
[[2]]
k
ij Aa A Baa Ba B Caa
A-A 0 0 2 0 0 0
A-Aa 0 1 0 0 0 0
A-Ba 0 0 1 3 1 0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L, 0L, 1L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 2L,
1L, 0L, 2L, 1L, 0L, 1L, 0L, 0L, 0L, 3L, 3L, 1L, 0L, 0L, 1L, 0L,
1L, 1L, 0L, 1L, 1L, 1L, 3L, 1L, 0L, 3L, 2L, 5L, 2L, 1L, 1L, 2L,
0L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 2L, 1L, 2L, 0L, 0L, 1L, 0L, 14L,
3L, 0L, 5L, 0L, 3L, 5L, 1L, 2L, 0L, 0L, 1L, 1L, 1L, 1L, 6L, 1L,
0L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 5L, 3L, 1L, 7L, 0L, 2L, 0L, 1L,
3L, 0L, 0L, 2L, 0L, 0L, 0L, 6L, 0L, 0L, 12L), .Dim = c(25L, 6L
), .Dimnames = list(ij = c("A-Ba", "A-Baa", "Aa-A", "Aa-Ba",
"Aa-Baa", "B-A", "B-B", "B-Ba", "B-Baa", "B-Caa", "Ba-A", "Ba-B",
"Ba-Ba", "Ba-Baa", "Ba-Caa", "Baa-A", "Baa-Aa", "Baa-B", "Baa-Ba",
"Baa-Baa", "Baa-Caa", "Caa-B", "Caa-Ba", "Caa-Baa", "Caa-Caa"
), k = c("Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs",
"table"), call = xtabs(formula = n ~ ij + k, data = .)), structure(c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L,
0L, 1L, 1L, 0L, 1L, 2L, 0L, 0L, 0L, 1L, 0L, 2L, 0L, 1L, 0L, 2L,
2L, 2L, 1L, 0L, 2L, 1L, 0L, 2L, 0L, 1L, 2L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 0L, 3L, 0L, 3L, 4L, 0L, 1L, 6L, 1L, 1L, 2L, 0L, 0L, 3L,
1L, 5L, 1L, 2L, 2L, 1L, 1L, 0L, 0L, 1L, 0L, 13L, 3L, 1L, 8L,
2L, 1L, 2L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 6L, 0L, 0L, 7L, 0L, 0L,
0L, 0L, 6L, 1L, 0L, 5L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L,
5L, 3L, 0L, 14L), .Dim = c(22L, 6L), .Dimnames = list(ij = c("A-A",
"A-Aa", "A-Ba", "Aa-A", "B-B", "B-Ba", "B-Baa", "B-Caa", "Ba-B",
"Ba-Ba", "Ba-Baa", "Ba-Caa", "Baa-A", "Baa-Aa", "Baa-B", "Baa-Ba",
"Baa-Baa", "Baa-Caa", "Caa-B", "Caa-Ba", "Caa-Baa", "Caa-Caa"
), k = c("Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs",
"table"), call = xtabs(formula = n ~ ij + k, data = .)), structure(c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L, 2L, 1L, 2L, 1L, 2L, 1L, 0L, 4L, 2L, 0L, 3L, 0L, 0L,
2L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 3L, 5L, 0L, 2L, 0L,
0L, 2L, 4L, 2L, 0L, 0L, 0L, 2L, 3L, 0L, 2L, 0L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 14L, 4L, 1L, 3L, 1L, 0L, 2L, 3L, 2L, 1L, 0L, 1L,
0L, 2L, 1L, 0L, 9L, 1L, 0L, 7L, 0L, 0L, 0L, 0L, 3L, 3L, 0L, 6L,
0L, 0L, 2L, 0L, 2L, 3L, 0L, 0L, 1L, 0L, 0L, 0L, 7L, 3L, 0L, 12L
), .Dim = c(24L, 6L), .Dimnames = list(ij = c("A-A", "A-Baa",
"Aa-A", "B-A", "B-B", "B-Ba", "B-Baa", "B-Caa", "Ba-A", "Ba-Aa",
"Ba-B", "Ba-Ba", "Ba-Baa", "Ba-Caa", "Baa-A", "Baa-Aa", "Baa-B",
"Baa-Ba", "Baa-Baa", "Baa-Caa", "Caa-B", "Caa-Ba", "Caa-Baa",
"Caa-Caa"), k = c("Aa", "A", "Baa", "Ba", "B", "Caa")), class = c("xtabs",
"table"), call = xtabs(formula = n ~ ij + k, data = .)))