首页 > 解决方案 > R 新手,我在运行我的函数时遇到错误

问题描述

random_players <- function(K){
  R_two_players <- sample(c(1:13), 2, replace = FALSE) 
  #select two 2 players randomly
  i <- R_two_players[1] 
  j <- R_two_players[2]
  new_rating <- elo_Rating(i, j, K)
  # call the function from q-6 in order to have 
  # access to probability of lose or success of players i and j   
  return(new_rating)
} 

ratings_updated<-matrix(nrow=13, ncol=10000) # is in my global environment

simulate_tournament<-function(K=0.02,n=10000) {  #n is the number of games we are simulating 
ratings_updated<-matrix(nrow=13, ncol=10000) 
for (i in 1:n) {
updated_rate<<-random_players(K)  #calling the function from function before to get the new_rating
ratings_updated[,i]<<-updated_rate
}
return(ratings_updated)
}

我收到“ratings_updated[, i] <- updated_rate 错误:矩阵上的下标数量不正确”。我希望每次进入循环时都会更新我的矩阵“ratings_updated”。

标签: rfunction

解决方案


推荐阅读