我已阅读 SO 线程: Reorder levels of a factor without changed order of values,效果很好。但是,在特定用例上,我对输出感到困惑:
> df$mode
[1] write read write_with_journal write read write_with_journal
[7] write read write_with_journal
Levels: read write write_with_journal
现在,我将因子顺序从“ read write write_with_journal
”更改为"write read write_with_journal
“:
> factor(df$mode, levels = c('write', 'read', 'write_with_journal'))
[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
Levels: write read write_with_journal
请注意,所有先前的类别值“write”、“read”等都被替换为 NA。我不确定为什么会这样。如果我手动创建数据框(而不是从文件中读取),如下所示:
> p = factor(rep(c("write", "read", "write_with_journal"),3))
> factor(p, levels = c('write', 'read', 'write_with_journal'))
然后一切都很好。为什么?