因素崩溃

时间:2019-08-01 16:00:51

标签: r forcats

使用forcats :: fct_collapse分解因子水平会导致意外结果

它遵循了fct_collapse示例中的一些修改代码

require(forcats)
partyid2 <- fct_collapse(gss_cat$partyid,
                         missing = c("No answer"),
                         other = "Other party",
                         rep = c("Strong republican", "Not str republican"),
                         ind = c("Ind,near rep", "Independent", "Ind,near dem"),
                         dem = c("Not str democrat", "Strong democrat"),
                         group_other = TRUE
)
table(gss_cat$partyid, partyid2)

例如,为什么“坚强的民主人士”级别最终变成“其他”级别?

非常感谢您提供提示,我做错了。

partyid2
                     missing other  rep  ind  dem Other
  No answer              154     0    0    0    0     0
  Don't know               0     1    0    0    0     0
  Other party              0     0  393    0    0     0
  Strong republican        0     0 2314    0    0     0
  Not str republican       0     0    0 3032    0     0
  Ind,near rep             0     0    0 1791    0     0
  Independent              0     0    0 4119    0     0
  Ind,near dem             0     0    0    0 2499     0
  Not str democrat         0     0    0    0 3690     0
  Strong democrat          0     0    0    0    0  3490

1 个答案:

答案 0 :(得分:0)

示例中的代码不正确。它更改顺序。保持顺序相同

{'a': 123, 'b': 456}