gpt4 book ai didi

r - 向量化函数中 sapply() 的意外行为

转载 作者:行者123 更新时间:2023-12-04 14:04:09 26 4
gpt4 key购买 nike

我想尝试一下模块化算法并编写了一些看似无辜的函数......但对以下意想不到的行为感到非常惊讶:

crt <- function(x, mods = c(5, 7)) {
sapply(mods, \(y) x %% y)
}
crt <- Vectorize(crt)

crt(20)
## [,1]
## [1,] 0
## [2,] 6

crt(55)
## [,1]
## [1,] 0
## [2,] 6

crt(1:100)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
## [1,] 1 2 3 4 0 1 2 3 4 0 1 2 3 4
## [2,] 1 2 3 4 5 6 0 1 2 3 4 5 6 0
## [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
## [1,] 0 1 2 3 4 0 1 2 3 4 0 1
## [2,] 1 2 3 4 5 6 0 1 2 3 4 5
## [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]
## [1,] 2 3 4 0 1 2 3 4 0 1 2 3
## [2,] 6 0 1 2 3 4 5 6 0 1 2 3
## [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]
## [1,] 4 0 1 2 3 4 0 1 2 3 4 0
## [2,] 4 5 6 0 1 2 3 4 5 6 0 1
## [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62]
## [1,] 1 2 3 4 0 1 2 3 4 0 1 2
## [2,] 2 3 4 5 6 0 1 2 3 4 5 6
## [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74]
## [1,] 3 4 0 1 2 3 4 0 1 2 3 4
## [2,] 0 1 2 3 4 5 6 0 1 2 3 4
## [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86]
## [1,] 0 1 2 3 4 0 1 2 3 4 0 1
## [2,] 5 6 0 1 2 3 4 5 6 0 1 2
## [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]
## [1,] 2 3 4 0 1 2 3 4 0 1 2 3
## [2,] 3 4 5 6 0 1 2 3 4 5 6 0
## [,99] [,100]
## [1,] 4 0
## [2,] 1 2

crt(x = 1:100, mods = c(12, 60))
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 1 14 3 16 5 18 7 20 9 22 11 24 1
## [26] 26 3 28 5 30 7 32 9 34 11 36 1 38 3 40 5 42 7 44 9 46 11 48 1 50
## [51] 3 52 5 54 7 56 9 58 11 0 1 2 3 4 5 6 7 8 9 10 11 12 1 14 3
## [76] 16 5 18 7 20 9 22 11 24 1 26 3 28 5 30 7 32 9 34 11 36 1 38 3 40

为什么最后一个函数调用 crt(x = 1:100, mods = c(12, 60)) 给出了完全不同的输出?第一个向量化输出 crt(1:100) 是我想要和期望的,最后一个在结构上似乎没有什么不同,但结果是......为什么?我该如何解决这个问题以获得与第一个相同的输出?

最佳答案

根据?Vectorize

The arguments named in the vectorize.args argument to Vectorize are the arguments passed in the ... list to mapply. Only those that are actually passed will be vectorized; default values will not.

这里,在 OP 的函数中,'mods' 有一个默认值。如果我们删除它

crt <- function(x, mods) {
sapply(mods, \(y) x %% y)
}
crt <- Vectorize(crt)

-测试

> crt(1:100, mods = c(5, 7))
[1] 1 2 3 4 0 6 2 1 4 3 1 5 3 0 0 2 2 4 4 6 1 1 3 3 0 5 2 0 4 2 1 4 3 6 0 1 2 3 4 5 1 0 3 2 0 4 2 6 4 1 1 3 3 5 0 0 2 2 4 4 1 6 3 1 0 3 2 5 4 0 1 2 3 4
[75] 0 6 2 1 4 3 1 5 3 0 0 2 2 4 4 6 1 1 3 3 0 5 2 0 4 2
> crt(1:100, mods = c(12, 60))
[1] 1 2 3 4 5 6 7 8 9 10 11 12 1 14 3 16 5 18 7 20 9 22 11 24 1 26 3 28 5 30 7 32 9 34 11 36 1 38 3 40 5 42 7 44 9 46 11 48 1
[50] 50 3 52 5 54 7 56 9 58 11 0 1 2 3 4 5 6 7 8 9 10 11 12 1 14 3 16 5 18 7 20 9 22 11 24 1 26 3 28 5 30 7 32 9 34 11 36 1 38
[99] 3 40

这里的输出格式在两个级别上确定 - 1) sapply 默认使用 simplify = TRUEVectorize 默认情况下有 SIMPLIFY = TRUE

此外,根据定义的函数,Vectorize 并不是内部真正需要的,它使用 *apply 函数进行循环,我们已经有了 crt sapply 定义,它循环遍历“mods”。在这些参数上应用 %% 的函数是 %%,默认情况下它已经是矢量化函数。

关于r - 向量化函数中 sapply() 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69079269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com