r中是否有必要使用因子到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:一个或多个因子的列表,每个因子的长度与X相同.元素通过as.factor强制转换为因子.
INDEX: list of one or more factors, each of same length as X. The elements are coerced to factors by as.factor.
据我了解,这意味着我必须使用INDEX
参数作为因子.在我的示例中,尽管INDEX
自变量不是因素,但结果可正确打印.
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.
这是因为R文档错误还是我误解了R文档?
Is this because R Documentation is wrong or I misunderstand R Documentation?
推荐答案
否.该文档说,INDEX
将被强制转换为一个因素,如果不容易的话.因此,如果您输入一个因子,则tapply
将立即使用它.如果没有,tapply
将首先强制执行.
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中是否有必要使用因子到INDEX参数作为tapply?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!