Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 R中i * (i-1)为i = 1to创建元素向量的最快方法是什么?i = n
i * (i-1)
i = 1
i = n
我确信使用 可能会有更快的解决方案Rcpp,但为简单起见,使用 base R
Rcpp
n <- 5 x <- seq_len(n) x[-1] * x[-n] ## [1] 2 6 12 20
或许像这样:
x = 1:10 x[-1] * x[-length(x)]
已经有一个基本功能可以做到这一点:
cumprod(x)