从列表中提取的矩阵保留列表数据类型

Matrix extracted from list keep list data type

我有一个仅由一个矩阵组成的列表:

$`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

矩阵是从"clickstream"包的"fitMarkovchain"函数导出的:

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

#extract list of matrix
tr<-mc@transitions

我的 objective 是对上面的矩阵进行垂直整形,rows/labels 名称和概率分别在一个单独的列中。我尝试了以下方法:

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

但尽管 as.matrix 转换,tr 对象似乎仍保留列表数据类型。

期望的输出是:

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

关于如何删除列表类型并垂直重塑矩阵的任何提示?

但是 tr 对象是

如果已经是矩阵,那么可以直接用t()进行转置。不知道下面是不是你想要的

t(tr)

这样

> 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

数据

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")))

这是重塑您的 table

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

这样

> 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