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 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).
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With