-1

我已阅读 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'))

然后一切都很好。为什么?

4

1 回答 1

1

您可能在所有因子水平上都有训练空间。做这个:

df$mode <- factor( gsub(" ", "", df$mode), 
                   levels = c('write', 'read', 'write_with_journal'))

或者(但不纠正问题)你可以这样做:

df$mode <- factor( df$mode, 
                   levels = levels(df$mode)[c(2,1,3)] )

只是为了证明这是合理的(尽管绝对不是问题,因为确实存在其他非打印字符):

> p = factor(rep(c("write ", "read ", "write_with_journal "),3))
>  factor(p, levels = c('write', 'read', 'write_with_journal'))
[1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
Levels: write read write_with_journal
于 2013-08-22T20:35:27.797 回答