samedi 11 juillet 2020

Make a particular variable's values appear in an R df column as a condition of the text in another column

I have a data frame with a column that contains text and a number of variables that contain values that correspond to the strength of that league. I want to create a new column in the same df that contains the corresponding variable value for each row.

I tried this:

prospects2020$leaguemod <- if(prospects2020$LEAGUE == "QMJHL") {
  QMJHLe
} else if (prospects2020$LEAGUE == "OHL") {
  OHLe
}else{
  WHLe
}

where QMJHLe, OHLe and WHLe are variables in my GE, and it creates the column but every value is equal to QMJHLe's value. I'm getting the error message:

Warning message:

In if (prospects2020$LEAGUE == "QMJHL") { : the condition has length > 1 and only the first element will be used

I feel like this should work but I'm pretty new to this. Maybe this is a wrong-headed way to accomplish what I am trying to do? How can I make it behave as I designed, or should I be using an entirely different approach?

Aucun commentaire:

Enregistrer un commentaire