Is it necessary to use factor to INDEX argument fo

2019-09-20 10:14发布

问题:

This question already has an answer here:

  • Grouping functions (tapply, by, aggregate) and the *apply family 9 answers
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"

In R Documentation,

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

As I understand, it means I have to use INDEX argument as factor. In my example, although INDEX argument is not factor, the result is correctly printed.

Is this because R Documentation is wrong or I misunderstand R Documentation?

回答1:

No. The documentation says that INDEX will be coerced to a factor, if it is not readily one. So, if you pass in a factor, then tapply will use it immediately; if not, tapply will do coercing first.



标签: r tapply