tapply 中没有回收 - R - 为什么?

问题描述 投票:0回答:1

我想知道 R 中回收的确切规则是什么。 因为有些函数如 cbind 会回收,但 tapply 不会。

我尝试过:

tapply(bb, aa, mean)
#> Error in tapply(bb, aa, mean) : arguments must have same length

我喜欢语言的正交性,但在这种情况下我找不到解释。

我期望 R 回收最短的向量。

r tapply recycle
1个回答
0
投票

这是一个如何使用rep函数来进行自己的回收的示例。

tapply (1:10,c(1:5,1:5),sum) 
# 1  2  3  4  5 
# 7  9 11 13 15 
tapply (1:10,c(1:5),sum)
# Error in tapply(1:10, c(1:5), sum) : arguments must have same length]

recyle_x_by_y <- \(x,y)rep(x,length.out=length(y))

tapply (1:10,recyle_x_by_y(1:5,1:10),sum)
# 1  2  3  4  5 
# 7  9 11 13 15 

Use at your own risk, I'd imagine its easy to make an improper use of recycling here if used without care, I would guess thats why efforts werent made to directly support it in tapply. 
© www.soinside.com 2019 - 2024. All rights reserved.