I'm new to R and I learned for loops today. I'm having an issue with running this loop. The dataframe should be linked here [1]: https://i.stack.imgur.com/ZpbFG.jpg The df is called scores, and it has the names of the home and away countries that played in the world cup. It also shows the name of the country that won. When it was a tie, though, there is an NA. I tried to run my code with just if (homename = winnername), then i would add TRUE to the vector, since I am looking at whether the home team won. And if not (else) then put FALSE. I tried it another way where I mentioned the NA specifically and it gives me the same error:
missing value where TRUE/FALSE needed
But my code is as follows,
scorevector <- c()
for (i in 1:nrow(scores)) {
if (scores$home_country[i] == scores$win_country[i]) {
scorevector <- c(scorevector, TRUE)
} else if (scores$win_country[i] == NA) {
scorevector <- c(scorevector, FALSE)
} else {
scorevector <- c(scorevector, FALSE)
}
}
This was the other version
scorevector <- c()
for (i in 1:nrow(scores)) {
if (scores$home_country[i] == scores$win_country[i]) {
scorevector <- c(scorevector, TRUE)
} else {
scorevector <- c(scorevector, FALSE)
}
}
Any help would be appreciated :)
Aucun commentaire:
Enregistrer un commentaire