【发布时间】:2017-01-10 17:29:16
【问题描述】:
我在 R 中进行这种递归操作(卷积),只需像俄罗斯娃娃一样将一个函数嵌套到另一个函数中。问题是是否有更优雅的方式来做到这一点。
首先肯定有更好的方法来设置以下输入向量:
ones = c(1, 1, 1, 1, 1, 1)
twos = c(1, 0, 1, 0, 1, 0)
threes = c(1, 0, 0, 1, 0, 0)
fours = c(1, 0, 0, 0, 1, 0)
实际的行是:
round(convolve(convolve(convolve(ones, rev(twos), type="open"), rev(threes), type="open"), rev(fours), type="open"))
[1] 1 1 2 3 5 6 6 8 8 8 6 6 5 3 2 1 1 0 0 0 0
【问题讨论】:
-
在基础R中有
Reduce(),在purrr中有reduce() -
@RichScriven 现在更正了错字。对此感到抱歉。