是否有必要在 r 中使用 factor to INDEX 参数进行 tapply?

Is it necessary to use factor to INDEX argument for tapply in r?

x

#X Income Commute Job.Growth Physicians
#1  A  26000    49.2       10.8       1987
#2  B  29300    45.3        9.5        517
#3  C  24800    39.8        8.2        592
#4  D  27900    46.8        7.6       3310
#5  E  37500    39.9       12.2        975
#6  A  26058    47.8       10.3        647
#7  B  33479    48.1       12.2        714
#8  C  28869    39.6       12.7        803
#9  D  37567    47.9       10.1        888
#10 E  30215    39.0       10.8        672
#11 A  38772    47.5       10.2        975
#12 B  34577    44.4       10.2        519
#13 C  39978    46.8       11.3        510
#14 D  34466    48.2        8.2        832
#15 E  29128    49.1       10.6        590



tapply(x$Income, x$X, mean)

#       A        B        C        D        E 
#30276.67 32452.00 31215.67 33311.00 32281.00 

str(x$X)

# chr [1:15] "A" "B" "C" "D" "E" "A" "B" "C" "D" "E" "A" "B" "C" "D" "E"

在 R 文档中,

INDEX: list of one or more factors, each of same length as X. The elements are coerced to factors by as.factor.

据我了解,这意味着我必须使用 INDEX 参数作为因素。在我的示例中,虽然 INDEX 参数不是因子,但结果已正确打印。

这是因为 R 文档有误还是我误解了 R 文档?

没有。文档说 INDEX 将被强制转换为一个因素,如果它不是一个因素的话。所以,如果你传入一个因子,那么 tapply 将立即使用它;如果没有,tapply 将首先进行强制转换。