简体   繁体   English

循环进入功能R

[英]Loop into function R

is there any way of transforming a for loop into a function in R? 有什么办法可以将for循环转换为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. 在循环外部设置X和Y的值。

Many thanks 非常感谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM