如何连接因子而不将它们转换为整数级别?

2024-03-22

我很惊讶地发现 R 在连接向量时会将因子强制转换为数字。即使级别相同,也会发生这种情况。例如:

> facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> facs
[1] i       want    to      be      a       factor  not     an      integer
Levels: a an be factor i integer not to want
> c(facs[1 : 3], facs[4 : 5])
[1] 5 9 8 3 1

在 R 中执行此操作的惯用方法是什么(在我的情况下,这些向量可能非常大)?谢谢。


来自R 邮件列表 http://www.mail-archive.com/r-help@r-project.org/msg38360.html:

unlist(list(facs[1 : 3], facs[4 : 5]))

要“cbind”因素,请执行以下操作

data.frame(facs[1 : 3], facs[4 : 5])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何连接因子而不将它们转换为整数级别? 的相关文章

随机推荐