我有一個函數需要循環兩個參數。據我所知,apply()
只能應用於一個具有維度指標的數組參數。我不知道有沒有應用兩個數組參數?這裏有一個例子:對具有兩個參數的函數應用()以應用
matrix_a <- matrix(1:6,3,2)
matrix_b <- matrix(2:7,3,2)
fun1 <- function(par1,par2){
mean(par1+par2) #true function are more complex than this
}
result <- numeric(nrow(matrix_a))
#this for loop give me exactly what I want, however, is there any sophistical way to do this? Like use a apply() function
for(i in 1:nrow(matrix_a)){
result[i] <- fun1(matrix_a[i,], matrix_b[i,])
}