gpt4 book ai didi

r - 矩阵和表名/dimnames

转载 作者:行者123 更新时间:2023-12-04 02:16:31 26 4
gpt4 key购买 nike

可能我的问题在某个地方得到了回答,但我在提问之前已经使用了我的搜索资源。

我在 R 中有一个 sample 表:

        munic
Gender Mun1 Mun2
female 146980 285797
male 140436 270084

当我使用 dimnames(sample) 时,我得到以下信息:

> dimnames(sample)
$Gender
[1] "female" "male"

$munic
[1] "Mun1" "Mun2"

我想创造一个一模一样的。

所以我做了以下事情:

Mat<-matrix(c(148470,24721,22829,24777,26137,43169,49613,40406,48337,34296,19492,+
176712, 27406, 23010, 25487, 27064, 48349, 52140, 44335, 50908, 35814, 18825), nrow=2)

colnames(Mat) <-c("mun_5","mun_1","mun_2","mun_3","mun_4","mun_6","mun_7","mun_8","mun_9","mun_10","mun_11")
rownames(Mat) <- c("Male", "Female")

Mat<-as.table(Mat)

但是我无法让它显示变量的通用标题。

我尝试了很多东西但没有帮助。

当我尝试获取 dimnames 时,我得到以下信息:

> dimnames(Mat)
[[1]]
[1] "Male" "Female"

[[2]]
[1] "mun_5" "mun_1" "mun_2" "mun_3" "mun_4" "mun_6" "mun_7" "mun_8" "mun_9" "mun_10" "mun_11"

有什么方法可以添加“未知”属性吗?

最佳答案

您需要将对象的属性设置为适当长度的列表。

attr(mat, "dimnames") <- list(Gender = c("Male", "Female"), 
munic = c("mun_5","mun_1","mun_2","mun_3","mun_4","mun_6","mun_7","mun_8","mun_9","mun_10","mun_11"))

> mat
munic
Gender mun_5 mun_1 mun_2 mun_3 mun_4 mun_6 mun_7 mun_8 mun_9 mun_10 mun_11
Male 148470 22829 26137 49613 48337 19492 27406 25487 48349 44335 35814
Female 24721 24777 43169 40406 34296 176712 23010 27064 52140 50908 18825

关于r - 矩阵和表名/dimnames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33417674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com