如何设置矩阵的行标题和列标题?

4

我有一个矩阵,看起来像这样:

> m <- cbind( c(1, 0), c(1, 1) )
> rownames(m) <- c('ON', 'OFF')
> colnames(m) <- c('ON', 'OFF')
> m

    ON OFF
ON   1   1
OFF  0   1

如何为行和列提供标题名称?例如:
                  thermostat
                  ON OFF
motion_sensor ON   1   1
              OFF  0   1

我看了一下 ?dimnames,但不知道如何实现/理解这个。
1个回答

7
尝试使用namesdimnames是一个list。 在您的示例中,list元素没有名称,可以使用names进行分配。
 names(dimnames(m)) <- c('motion_sensor', 'thermostat')
 m
 #            thermostat
 #motion_sensor ON OFF
 #         ON   1   1
 #         OFF  0   1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接