user3036416
user3036416

Reputation: 1255

Loop into function R

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

Answers (1)

David Arenburg
David Arenburg

Reputation: 92282

testFunc <- function(x, X, Y){
  x * X * Y
}
test <- sapply(test, testFunc, X, Y)

Upvotes: 2

Related Questions