0
我有這樣的代碼:R - 如何將兩個`for`循環轉換爲`lapply`?
getSomething = function(x, y) {
return something
}
b = matrix(NA, nrow = ncol(a), ncol = ncol(a))
# Loop through the columns
for(i in 1:ncol(a)) {
# Loop through the columns for each column
for(j in 1:ncol(a)) {
b[i, j] = getSomething(as.matrix(a[i]), as.matrix(a[j]))
}
}
它工作得很好,但是當我嘗試運行大數據集的代碼,它需要很長的時間來運行。
如何將其轉換爲lapply
函數使其運行速度更快?
謝謝。
謝謝您的回答,但我仍然有錯誤消息:'變暗[產品3249]不匹配對象的長度[1]'(3249來自57 * 57,'a'有57個變量,有200個觀察值)。 我是否需要考慮'a'中的觀察次數? – cellardoors