R:表和多维数组中的维名称 [英] R: Dimension names in tables and multi-dimensional arrays
本文介绍了R:表和多维数组中的维名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于我使用的函数需要一个表对象作为参数,因此我想将多维数组转换为表,但是维名称存在问题.
Since a function that I use requires a table object as parameter, I would like to convert a multidimensional array to a table, but I have problems with dimension names.
如as.table
帮助文件中所指定,dnn
参数应包含暗名.
As specified in the help file of as.table
, the dnn
parameter should contain dimnames names.
dnn … the names to be given to the dimensions in the result (the dimnames names).
但是即使指定dnn
,由as.table
生成的表也没有维度名称.
But even when specifying dnn
, my tables produced by as.table
have no dimension names.
以下代码说明了我的问题.
The following code illustrates my problem.
>test <- table(c("a","b","c","c","c"),c("1","2","3","2","2"),dnn=c("letters","numbers"))
>test
numbers
letters 1 2 3
a 1 0 0
b 0 1 0
c 0 2 1
# this works perfectly
现在从数组构造表时尝试相同的方法:
now try the same when constructing the table from an array:
>my2dimdata <- array(c(1,0,0,0,1,2,0,0,1),dim=c(3,3),
dimnames=list(c("a","b","c"),
c("1","2","3")))
>my2dimdata
1 2 3
a 1 0 0
b 0 1 0
c 0 2 1
# the array as expected
>my2dimtable <- as.table(my2dimdata,dnn=c("letters","numbers"))
>my2dimtable
1 2 3
a 1 0 0
b 0 1 0
c 0 2 1
# there are no dimnames
推荐答案
as.table
没有dnn
参数.您需要手动设置名称.
as.table
doesn't have a dnn
argument. You need to set the dimnames manually.
my2dimdata <- array(c(1,0,0,0,1,2,0,0,1),dim=c(3,3),
dimnames=list(c("a","b","c"),
c("1","2","3")))
my2dimdata <- as.table(my2dimdata)
names(attributes(my2dimdata)$dimnames) <- c("letters","numbers")
# numbers
# letters 1 2 3
# a 1 0 0
# b 0 1 0
# c 0 2 1
这篇关于R:表和多维数组中的维名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文