lundi 16 juillet 2018

Creating a column whose values are dependent on multiple other columns

I'm trying to create a new column ("newcol") in a dataset ("data"), whose values will be determined by the contents of up to two other columns in the dataset ("B_stance" and "C_stance"). The values within B_stance are either "L", "R", "U" or "N". Within C_stance they are either "L" or "R".

Please excuse the semi-logical language, but I need R code which will achieve this for the contents of newcol:

if (data$B_stance = "L" AND data$C_stance = "L") then (data$newcol = "N")
if (data$B_stance = "L" AND data$C_stance = "R") then (data$newcol = "Y")
if (data$B_stance = "R" AND data$C_stance = "R") then (data$newcol = "N")
if (data$B_stance = "R" AND data$C_stance = "L") then (data$newcol = "Y")
if (data$B_stance = "U") then (data$newcol = "N")
if (data$B_stance = "N") then (data$newcol = "N")

I've tried to see if/how "ifelse" could achieve this, but cannot find an example of how to draw from multiple column values in determining the new value.

Aucun commentaire:

Enregistrer un commentaire