mercredi 15 mai 2019

Ifelse statemente with 4 conditions

With the following sample data, I'm trying to create a new column "NOTA_NUM" (value "0" or "1" or "2" or "3" or "4") in my dataframe(df) based on the values of four conditional variables ("A", "B", "C", "D", "E") from one existing column (column1).

I have already tried:

df$NOTA_NUM <- ifelse(rowSums(df[ , "column1"]=="A"), 0,
        ifelse(rowSums(df[ , "column1"]=="B"), 1,
               ifelse(rowSums(df[ ,"column1"]=="C"), 2,
                      ifelse(rowSums(df[ , "column1"]=="D",3,4))

but that haven't worked the way I would like.

I want "NOTA_NUM" to look like:

column1   NOTA_NUM
A             0
C             2
B             1
D             3
E             4

Aucun commentaire:

Enregistrer un commentaire