samedi 21 avril 2018

How do I properly format ifelse() statements in r? Facing an issue with test clause

I'm having what I believe to be a very basic issue but I am completely stuck.

I'm currently having an issue with a double for loop in r where I use one for loop to increment for one vector of data and the other for another different sized vector of data.

Right now, each element is listed as false in the voting_data$winMVP column, and I'm trying to get it to register true if the player has won the MVP in that specific year. To do this, I check to see if a player in the voting_data matrix has the same name and age as a player in the yearwinner_data matrix (all players in this matrix have won MVP). If their name and age matches up then that means the player at that age won MVP. For some reason however, the ifelse() statement does not produce any true outputs, making me wonder if there's something wrong with my if statement (the first parameter in the ifelse()). Does the code below look logically sound or is there an issue with the if else statement or incrementing for loop?

Any help is very much appreciated. Thank you.

for (i in 1:nrow(voting_data)) {
  for (t in 1:nrow(yearwinner_data)) {
    voting_data$winMVP[i] = 
        ifelse(voting_data$Player[i] == yearwinner_data$Player[t] &
           voting_data$Age[i] == yearwinner_data$Player[t], T, 
           ## if these aren't true then we need to make sure that it wasn't already
          ## shown true earlier
           ifelse(voting_data$winMVP[i]==T, T, F))
  }
}

Aucun commentaire:

Enregistrer un commentaire