由于大括号问题,我的代码无法正常工作
o1 = read.table('/Users/manshi/Desktop/PSYC57H3/PSYC57_Homework3/Object1.csv', header=TRUE, sep=",")
PredictedValue = vector(mode = 'numeric', length = 100)
PredictionError = vector(mode = 'numeric', length = 100)
PredictedValue[1] = 0
AlAccepts = vector(mode = 'logical', length = 100)
for (trial in 1:100){
ifelse (AlAccepts[trial] == FALSE, 0, 1) {
PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
} ifelse (AlAccepts[trial] == TRUE, 0, 1) {
PredictedValue[trial + 1] = PredictedValue[trial]
}
}
我收到的错误消息是:
> for (trial in 1:100){
+ ifelse (AlAccepts[trial] == FALSE, 0, 1) {
Error: unexpected '{' in:
"for (trial in 1:100){
ifelse (AlAccepts[trial] == FALSE, 0, 1) {"
> PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
> PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
> } ifelse (AlAccepts[trial] == TRUE, 0, 1) {
Error: unexpected '}' in " }"
> PredictedValue[trial + 1] = PredictedValue[trial]
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in "}"
>
我做错了什么?
答案 0 :(得分:0)
ifelse (AlAccepts[trial] == FALSE, 0, 1)
是一个函数,并返回一个返回值,在这种情况下为0或1.未执行其他条件执行,因此以下括号显示语法错误。使用else if(){}
应该按预期工作:
for (trial in 1:100){
if (AlAccepts[trial] == FALSE) {
PredictionError[trial] = o1$Reward[trial] - PredictedValue[trial]
PredictedValue[trial + 1] = PredictedValue[trial] + .3*PredictionError[trial]
} else if(AlAccepts[trial] == TRUE) {
PredictedValue[trial + 1] = PredictedValue[trial]
}
}