首页 > 解决方案 > R新手,在我的函数中收到错误

问题描述

true_RA<-seq(-2,2 ,length.out = 13)  #true vector with -2<RA<2 in
# even intervals for 13 players 

true_RA

current_RA<-true_RA   #assign true_RA to the current ratings

ratings_overtime<-matrix(current_RA, nrow=13, ncol=10,000) #create a #matrix of 13 players
# and 10000 ratings 

p<-0.5825702

simulate_game<-function(i,j){
    old_RA_i<-current_RA[i] #assign the rate from 
#my current rating vector from vector 

    old_RA_j<-current_RA[j]
    win<rbinom(1,1,p)
if (win==1){current_RA_i<-old_RA_j+400}
else{current_RA_j<-old_RA_i+400}
}  
 return(c(current_RA_i,current_RA_j))
}

我的函数应该模拟玩家 i 和 j 之间的游戏,因为他们的真实基础评分来自rbinom(1,1,p). 我收到一个错误:找不到对象“current_RA_i”。

标签: rfunction

解决方案


You have an extra close curly bracket, so the function ends before the return statement.

Since the current_RA_i and current_RA_j variables are defined within the function, they are not global in scope, and thus have no value after the function ends.

Removing the } character just before your return statement should resolve the error.


推荐阅读