1
有什麼方法可以實現以下矢量化?向量化這個for循環:將函數應用於子索引?
# x: some vector
# index: some vector of indeces
n <- length(index)
y <- rep(NA, n)
for (i in 1:n) {
y[i] = myfunction(x[1:index[i])
}
基本上,我想申請myfunction
到矢量x
的各種子集。它看起來並不像apply
函數是用來處理這個問題的。
謝謝。這樣可行。正是我在找什麼。 – lowndrul 2011-03-16 02:59:40