R: Dimension names in tables and multi-dimensional arrays

后端 未结 1 892
情话喂你
情话喂你 2021-02-20 11:02

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.

相关标签:
1条回答
  • 2021-02-20 11:21

    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
    
    0 讨论(0)
提交回复
热议问题