I am trying to do something like the following in my coding: I have 3 variables (x, y, z) in my dataframe df. I want to create a new variable w, where it will be first populated with x if the value of x is not 1 or 2, however if x is 1 or 2, then w will be populated by y iff y is not 1 or 2, and lastly if y is 1 or 2, then w will take the value of z. So I wrote the following..
df$w <- ifelse((df$x!=1 | df$x!=2), df$x, ifelse((df$y!=1 | ds$y!=2), df$y, df$z))
But, this does not seem to work, the variable w takes the value of x but after that fails to go to y and check the condition and proceed to z. I have tried a few other techniques but no matter how I tweak this, it doesn't work! Any help will be greatly appreciated! Please point out to me if this is a repeat question, though I could not find anything close (I may be totally off track my searching capability!)!
Aucun commentaire:
Enregistrer un commentaire