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

☆樱花仙子☆ 提交于 2020-01-26 04:04:23

问题


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.



来源:https://stackoverflow.com/questions/38493940/is-it-necessary-to-use-factor-to-index-argument-for-tapply-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!