I am a beginner in R and I can't run my program well. I think the problem is due to the operators but I might be wrong... Any help would be greatful !
I know this topic has already been discussed here: R - boolean operators && and || but I still don't have an answer.
Here are my data: data
And my program:
################## Model Input ##################
fact <- 1.46
file_r <- file2[file2$G == 0,] #delete the row where G == 1
C_Agents <- sum(file2$G == 1 & file2$A == "Tiger")
NC_Agents <- sum(file2$G == 1 & file2$A == "Lion")
################## Model Run ##################
for (i in nrow(file_r)) {
i <- i+1
rand2 <- 0.2
Affinity <- file_r$C/file_r$E + (rand2 *file_r$F*file_r$H)
if((file_r$A == "Tiger") && (Affinity < (file_r$B) * (file_r$D))){
Tiger_lost <- sum(file_r$A == "Tiger")
file_r$A <- "Lion" #switch Tiger to Lion
} else if((file_r$A == "Lion") && (Affinity < (file_r$B) * (file_r$D) * fact)){
Tiger_gained <- sum(file_r$A == "Lion")
file_r$A <- "Tiger" #switch Lion to Tiger
}
}
################## Model Output ##################
C_Agents
NC_Agents
Tiger_lost
Tiger_gained
The result is:
> C_Agents
[1] 2
> NC_Agents
[1] 5
> Tiger_lost
[1] 3
> Tiger_gained
Error: object 'Tiger_gained' not found
The problems is: - the loop if else is never never reached wheareas it is supposed to be reached once error
Aucun commentaire:
Enregistrer un commentaire