在因子变量上使用nchar函数

3

有人能解释一下这里到底发生了什么吗?当一个变量被编码为因子(factor)并且nchar强制转换为字符时,为什么该函数不能有效地计算字符数?

> x <- c("73210", "73458", "73215", "72350")
> nchar(x)
[1] 5 5 5 5
> 
> x <- factor(x)
> nchar(x)
[1] 1 1 1 1
> 
> nchar(as.character(x))
[1] 5 5 5 5

thanks.


如果您键入?nchar,您将看到它作用于字符向量,而不是类因子的向量。它还会作用于数字向量,但预测性较差[请参见nchar(mtcars$disp)]。 - Tyler Rinker
2
stringr中的str_length函数避免了这个烦人的错误(以及令人讨厌的NA行为)。 - hadley
3个回答

5

因为有因素,你的数据会被表示为1、2等。你想做的是计算级别的字符数:

> nchar(levels(x)[x])
[1] 5 5 5 5

2
请查看?factor的警告部分:
The interpretation of a factor depends on both the codes and the
 ‘"levels"’ attribute.  Be careful only to compare factors with the
 same set of levels (in the same order).  In particular,
 ‘as.numeric’ applied to a factor is meaningless, and may happen by
 implicit coercion.  To transform a factor ‘f’ to approximately its
 original numeric values, ‘as.numeric(levels(f))[f]’ is recommended
 and slightly more efficient than ‘as.numeric(as.character(f))’.

nchar(levels(x))

2
其他答案是正确的,我认为问题在于nchar检查的是底层整数代码,而不是标签。然而,我认为最直接回答你的问题的是来自? nchar 的这段内容:
“在 x 上执行了作为字符的默认方法的内部等效物(因此没有方法分派)”
我不100%确定,但我怀疑这意味着在nchar中发生的强制转换与直接调用as.character时发生的不同,很可能直接转到整数代码,而不是“聪明地”查看标签。

这实际上听起来像是一个 bug,或者至少是意料之外的行为。 - Hong Ooi
@HongOoi:是的,或者更可能的是一个很老的bug,以至于代码依赖它,所以它已经成为了一个特性。 - Richie Cotton
@HongOoi 这也是我的第一反应,但我在 R 的 help/dev 邮件列表上潜伏了足够长的时间,知道除非我真的非常确定,否则最好不要建议它。;) - joran
1
可能需要在 R-fortunes 中添加另一个条目,类似于“因子确实是非常奇怪的生物”。 - Carl Witthoft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接