矩阵和表名/暗名 [英] matrix and table names / dimnames
本文介绍了矩阵和表名/暗名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题可能在某个地方回答了,但是在问之前我已经使用了搜索资源.
Probably my question is answered somewhere but I have used my searching resources before asking.
我在 R 中有一个sample
表:
munic
Gender Mun1 Mun2
female 146980 285797
male 140436 270084
当我使用dimnames(sample)
时,我得到以下信息:
When I use dimnames(sample)
I get the following:
> dimnames(sample)
$Gender
[1] "female" "male"
$munic
[1] "Mun1" "Mun2"
我想创建一个完全一样的东西.
And I want to create one exactly alike.
所以我执行以下操作:
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)
但是我不能让它显示变量的通用名称.
However I cannot make it show the variables' generic title so to say.
我尝试了很多事情,但没有帮助.
I tried many things but not helped.
当我尝试获取暗号时,我得到以下信息:
When I try to get the dimnames I get the following:
> 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"
有什么方法可以添加未知"属性?
Is there any way to add the "unknown" attributes?
推荐答案
您需要将对象的属性设置为适当长度的列表.
You need to set the attribute of the object to be a list of proper length.
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
这篇关于矩阵和表名/暗名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文