R-“无法将类别“” msm.est“”强制转换为data.frame“

时间:2019-08-28 15:16:56

标签: r

我是R的面向对象设置的新手,所以轻松一点:)

我正在使用msm library,尤其是pmatrix function。此函数正确运行并在调用时打印矩阵:

t = pmatrix.msm(x=fb.msm.tech)

print(t)

            State 1      State 2   State 3    State 4    State 5
State 1 0.215414273 0.0177223229 0.6120709 0.08127077 0.07352177
State 2 0.033494683 0.6421517256 0.2223060 0.02454739 0.07750024
State 3 0.007500551 0.0001843707 0.7530770 0.15781630 0.08142178
State 4 0.008522211 0.0002170102 0.2993561 0.57798320 0.11392148
State 5 0.000000000 0.0000000000 0.0000000 0.00000000 1.00000000

t是类msm.est的对象,但我想将其转换为data.frame(或矩阵)。我该怎么做?这会导致错误:

> data.frame(t)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"msm.est"’ to a data.frame

以下是有关t的一些属性;让我知道您是否需要更多信息:

> typeof(t)
[1] "double"

> str(t)
Error in unclass(x)[i, j] : subscript out of bounds

> class(t)
[1] "msm.est"

> methods(class=class(t))
[1] [     print
see '?methods' for accessing help and source code

> dput(t)
structure(c(0.215414272798276, 0.0334946826176258, 0.00750055145168075, 
0.00852221143845429, 0, 0.0177223229119804, 0.642151725588598, 
0.00018437071164374, 0.000217010203854516, 0, 0.612070862567051, 
0.222305955350619, 0.753077002693833, 0.299356094968411, 0, 0.0812707673639357, 
0.024547392341985, 0.157816299874524, 0.577983203836297, 0, 0.0735217743587564, 
0.0775002441011723, 0.081421775268318, 0.113921479552984, 1), .Dim = c(5L, 
5L), .Dimnames = list(c("State 1", "State 2", "State 3", "State 4", 
"State 5"), c("State 1", "State 2", "State 3", "State 4", "State 5"
)), class = "msm.est")

1 个答案:

答案 0 :(得分:0)

msm.est对象基本上只是具有特殊类的矩阵。您可以删除该类以获得矩阵,然后使用data.frame()将矩阵转换为data.frame。

data.frame(unclass(t))