Search code examples
rmatrixreshape2

Matrix extracted from list keep list data type


I have a list composed by just one matrix:

$`1`
                   Buy  C-Level_3RDLIVE   C-Level_3RDWP   C-Level_AR   C-Level_ARCHWEB   C-Level_ASKOD   C-Level_CR 
 Buy                 0         0.1818182       0.0000000            0                 0               0            0
 C-Level_3RDLIVE     0         0.0000000       0.0000000            0                 0               0            0
 C-Level_3RDWP       0         0.0000000       0.1111111            1                 0               0            0
 C-Level_AR          0         0.0000000       0.0000000            0                 0               0            0
 C-Level_ARCHWEB     0         0.0000000       0.0000000            0                 0               0            1

The matrix it is derived from the "fitMarkovchain" function of the "clickstream" package:

#fit the model
mc <- fitMarkovChain(clickstreamList = cls, order = 1,
                     control = list(optimizer = "quadratic"))
mc

#extract list of matrix
tr<-mc@transitions

My objective is to have the above matrix reshaped vertically, with the rows/labels names and probabilities each in a separate column. I have tried the following:

tr<-tr[[1]]
rwn<-rownames(tr)
as.data.frame(t(as.matrix((tr))))

But it seems the tr object keep the list data type despite the as.matrix transformation.

The desired output is:

x1   x2                  %
Buy Buy                  0
Buy C-level_3Rdlive      0.1818182
..  ..                   ..

Any hint about how to remove the list type and reshaped the matrix vertically?

But the tr object is


Solution

  • If it is already a matrix, then you can directly use t() for the transpose. I am not sure if things below are what you want

    t(tr)
    

    such that

    > t(tr)
                          Buy C-Level_3RDLIVE C-Level_3RDWP C-Level_AR C-Level_ARCHWEB
    Buy             0.0000000               0     0.0000000          0               0
    C-Level_3RDLIVE 0.1818182               0     0.0000000          0               0
    C-Level_3RDWP   0.0000000               0     0.1111111          0               0
    C-Level_AR      0.0000000               0     1.0000000          0               0
    C-Level_ARCHWEB 0.0000000               0     0.0000000          0               0
    C-Level_ASKOD   0.0000000               0     0.0000000          0               0
    C-Level_CR      0.0000000               0     0.0000000          0               1
    

    DATA

    tr <- structure(c(0, 0, 0, 0, 0, 0.1818182, 0, 0, 0, 0, 0, 0, 0.1111111, 
    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    1), .Dim = c(5L, 7L), .Dimnames = list(c("Buy", "C-Level_3RDLIVE", 
    "C-Level_3RDWP", "C-Level_AR", "C-Level_ARCHWEB"), c("Buy", "C-Level_3RDLIVE", 
    "C-Level_3RDWP", "C-Level_AR", "C-Level_ARCHWEB", "C-Level_ASKOD", 
    "C-Level_CR")))
    

    Here is the code to reshape your table

    X <- data.frame(rev(expand.grid(rownames(tr),colnames(tr))),val = as.vector(t(tr)))
    

    such that

    > X
                  Var2            Var1              val
    1              Buy             Buy        0.0000000
    2              Buy C-Level_3RDLIVE        0.1818182
    3              Buy   C-Level_3RDWP        0.0000000
    4              Buy      C-Level_AR        0.0000000
    5              Buy C-Level_ARCHWEB        0.0000000
    6  C-Level_3RDLIVE             Buy        0.0000000
    7  C-Level_3RDLIVE C-Level_3RDLIVE        0.0000000
    8  C-Level_3RDLIVE   C-Level_3RDWP        0.0000000
    9  C-Level_3RDLIVE      C-Level_AR        0.0000000
    10 C-Level_3RDLIVE C-Level_ARCHWEB        0.0000000
    11   C-Level_3RDWP             Buy        0.0000000
    12   C-Level_3RDWP C-Level_3RDLIVE        0.0000000
    13   C-Level_3RDWP   C-Level_3RDWP        0.0000000
    14   C-Level_3RDWP      C-Level_AR        0.0000000
    15   C-Level_3RDWP C-Level_ARCHWEB        0.0000000
    16      C-Level_AR             Buy        0.0000000
    17      C-Level_AR C-Level_3RDLIVE        0.1111111
    18      C-Level_AR   C-Level_3RDWP        1.0000000
    19      C-Level_AR      C-Level_AR        0.0000000
    20      C-Level_AR C-Level_ARCHWEB        0.0000000
    21 C-Level_ARCHWEB             Buy        0.0000000
    22 C-Level_ARCHWEB C-Level_3RDLIVE        0.0000000
    23 C-Level_ARCHWEB   C-Level_3RDWP        0.0000000
    24 C-Level_ARCHWEB      C-Level_AR        0.0000000
    25 C-Level_ARCHWEB C-Level_ARCHWEB        0.0000000
    26   C-Level_ASKOD             Buy        0.0000000
    27   C-Level_ASKOD C-Level_3RDLIVE        0.0000000
    28   C-Level_ASKOD   C-Level_3RDWP        0.0000000
    29   C-Level_ASKOD      C-Level_AR        0.0000000
    30   C-Level_ASKOD C-Level_ARCHWEB        0.0000000
    31      C-Level_CR             Buy        0.0000000
    32      C-Level_CR C-Level_3RDLIVE        0.0000000
    33      C-Level_CR   C-Level_3RDWP        0.0000000
    34      C-Level_CR      C-Level_AR        0.0000000
    35      C-Level_CR C-Level_ARCHWEB        1.0000000