【发布时间】:2019-02-23 20:44:23
【问题描述】:
我有一个数据集:
df <- structure(list(gender = c("female", "male", NA, NA, "male", "male",
"male"), Division = c("South Atlantic", "East North Central",
"Pacific", "East North Central", "South Atlantic", "South Atlantic",
"Pacific"), Median = c(57036.6262, 39917, 94060.208, 89822.1538,
107683.9118, 56149.3217, 46237.265), first_name = c("Marilyn",
"Jeffery", "Yashvir", "Deyou", "John", "Jose", "Daniel")), row.names = c(NA,
-7L), class = c("tbl_df", "tbl", "data.frame"))
我需要执行分析,以便我不能在 gender 变量中包含 NA 值。其他列太少并且没有已知的预测值,因此实际上不可能估算这些值。
我可以通过完全删除不完整的观察结果来执行分析 - 它们约占数据集的 4%,但我希望通过将 female 或 male 随机分配到缺失的案例中来查看结果。
除了编写一些非常丑陋的代码来过滤到不完整的情况,分成两部分并将NAs 替换为female 或male 在每一半中,我想知道是否有一种优雅的方式来随机或按比例为NAs 赋值?
【问题讨论】: