【问题标题】:Including NA Values in structable()在 structable() 中包含 NA 值
【发布时间】:2019-09-20 08:11:38
【问题描述】:

我想使用 vcd 包 structable 函数制作一个列联表。我选择的两个列(consent_aconsent_b)具有 NA 值以及因子(是,否)值,因为每个案例都可以被同意用于过程 a 或过程 b,但不能同时满足两者。例如,如果一个案件被同意执行程序 a,他们不会被要求执行程序 b(因此consent_b 将是 NA)。在列联表中,我想包括因素为是、否和不适用的所有情况。

library(vcd)

mydata <- data.frame(
  report_year = c(2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 2014, 2014),
  report_week = c(1, 1, 1,  1, 1, 2, 2, 2, 2, 2),
  consenta = c("Yes", "Yes", NA, "Yes", "No", "Yes", "Yes", NA, "Yes", "No"),
  consentb = c(NA, NA, NA, NA, "Yes", NA, NA, "Yes", NA, "No"))

epicurve <- as.data.frame(structable(proj11[, c("report_epiweek", "report_year", "consent_a", "consent_b")]))

【问题讨论】:

  • 请提供您的数据的minimal reproducible example。听起来好像您将NULLNA 或可能是值为“NULL”的字符串混淆了。这些不是一回事。
  • 对不起,你是对的。我混淆了NULL和NA。我在上面提供了额外的代码来重现我的数据。谢谢。

标签: r na vcd


【解决方案1】:

我不熟悉 vcd 包,但据我所知,在使用列联表时没有内置方法可以保留 NA 值。但是,您可以在保持NAs 和基本table() 函数的同时实现相同的扁平表。

as.data.frame(with(mydata, table(report_week, report_year, consent_a, consent_b, useNA = "ifany")))

如果此结果不适合您的后续步骤,那么您只需将数据集中的 NAs 替换为符号值,然后继续使用 vcd 包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多