【问题标题】:How do I convert the results of survey::syvratio into a dataframe?如何将调查结果::syvratio 转换为数据框?
【发布时间】:2020-05-20 18:17:42
【问题描述】:

我已经使用 svyratio 计算了一些变量的比率,但它不允许我将返回值转换为数据框。使用 DF1

"as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 不能强制类 '"svyratio"' 到 data.frame"

【问题讨论】:

  • 请用dput展示一个可重复的小例子

标签: r


【解决方案1】:

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多