as.data.frame 基本上没有默认方法。什么只有你看到的警告:
as.data.frame.default
#====== console output=====
function (x, ...)
stop(gettextf("cannot coerce class %s to a data.frame", sQuote(deparse(class(x))[1L])),
domain = NA)
<bytecode: 0x55e6ce5e1ff0>
<environment: namespace:base>
因此,除非 package::survey 的作者编写了 as 方法,否则您将看到该消息。如果您知道 svyratio 值的内容应该是一个包含两个元素的列表,那么您可以尝试将列表添加到 objects 类属性。 x 对象是使用 ?svyratio 帮助页面中的第一个示例构建的。
class(x) <- list("svyratio", "list")
> as.data.frame(x)
arrests arrests.1
alive 0.1535064 5.770992e-05
在这种情况下,svyratio 对象是两个矩阵的列表,但由于它们每个只有一个元素,所以强制继续进行:
> str(x)
List of 2
$ ratio: num [1, 1] 0.154
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "alive"
.. ..$ : chr "arrests"
$ var : num [1, 1] 5.77e-05
..- attr(*, "dimnames")=List of 2
.. ..$ : chr "alive"
.. ..$ : chr "arrests"
- attr(*, "call")= language svyratio.survey.design2(~alive, ~arrests, design = scddes)
- attr(*, "class")= chr [1:2] "svyratio" "list"