首页 > 解决方案 > 按名称划分 R 表维度

问题描述

我有一个 3Dtable()输出。如何按名称对 2D 表进行子集化?

dim1 <- LETTERS
dim2 <- letters
dim3 <- 1:26
tbl <- table(dim1, dim2, dim3)
names(attr(tbl, "dimnames")) # dim1, dim2 dim3

table(dim1, dim2) # how do I get this output without redoing the tabulation?

标签: rarraysmatrixmultidimensional-arraysubset

解决方案


您想使用margin.tablemarginSums。这两个功能是一样的。第二个更具描述性,但第一个是原始名称。要将 3 维数组折叠为 2 维:

margin.table(tbl, c("dim1", "dim2"))   # Or margin.table(tbl, 1:2)

会给你一样的

table(dim1, dim2)

推荐阅读