本文介绍了如何串联因子,而不将其转换为整数级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was surprised to see that R will coerce factors into a number when concatenating vectors. This happens even when the levels are the same. For example:

> 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中这样做的惯用方式是什么(在我的情况下,这些向量可能很大)?谢谢。

what is the idiomatic way to do this in R (in my case these vectors can be pretty large)? Thank you.

推荐答案

来自:

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

To'cbind '因素,

To 'cbind' factors, do

data.frame(facs[1 : 3], facs[4 : 5])

这篇关于如何串联因子,而不将其转换为整数级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:52