lundi 1 août 2016

R: Add column dependent on value in other column to data frame

I am attempting to add a column to my data frame where the value in that column depends on the value in another column for the length of the data frame. For example...

  for(i in 1:length(df)){
      if(df$x<8){df$y="a"
      }
      else{df$y="b"}}

This is not working. My column df$y is created and populated with "b" throughout, including the rows that contain df$x<8 which should have df$y="a". I am getting a warning that says"the condition has length > 1 and only the first element will be used" .

Any suggestions will be very much appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire