mercredi 5 juillet 2017

I am trying to create a new column that is conditional upon the contents of existing columns

I am trying to make several new columns in my data frame that are conditional upon the contents of a few existing columns.

In pseudo-code, the arguments basically go "If VariableA is 1 and either VariableB is 2 or VariableC is 3, then make VariableD = 1, and if it does not meet these conditions make it a zero.

I have tried using for loops and ifelse statements, but have had no luck. I know that the logic of my commands are correct, but I am making some error translating it into R syntax, which isn't surprising because I just started using R about a week ago.

Below is a simplified version of what I have tried doing...

Data$VariableD <- ifelse(Data$VariableA == 1 && (Data$VariableB == 2 || Data$VariableA == 3), 1, 0)

It runs without error, but upon examining the contents of VariableD, all cells are filled with "NA"

Aucun commentaire:

Enregistrer un commentaire