Reputation: 1255
is there any way of transforming a for loop into a function in R?
For example if I have
test <- array(0, dim = c(12))
for (i in 1:12 ){
test[i] <- test[i] * X * Y
}
setting outside the loop the value of X and Y.
Many thanks
Upvotes: 1
Views: 129
Reputation: 92282
testFunc <- function(x, X, Y){
x * X * Y
}
test <- sapply(test, testFunc, X, Y)
Upvotes: 2