mercredi 9 janvier 2019

Ifelse using And

I have a sample dataset below:

Col1    Col2    Col3
   A       6       9  
   B       7      10
   C       8      11
   C       9      12
   D      10      13

How do I do an ifelse statement that says - "If Col1 does not have the values A and B, then make Col3 = Col2, if not, keep original values."

Final expected output:

Col1    Col2    Col3
   A       6       9  
   B       7      10
   C       8       8
   C       9       9
   D      10      10

This was my attempt but it didn't work:

df$Col3 <- ifelse(df$Col1!="A" & df$Col1!="B", df$Col2, df$Col3)

Aucun commentaire:

Enregistrer un commentaire