mercredi 5 février 2020

Problem using if statement with multiple conditions to substitute factors in dataframe (R)

I have a data frame quiz that looks like this

Participant  Group  Test  Marks
1             1      Qz1   2.4
1             1      Qz2   3.2
1             1      Qz3   2
1             1      Qz4   1
2             1      Qz1    3
2             1      Qz2   4
2             1      Qz3   3
2             1      Qz4   4
3             2      Qz1   4
3             2      Qz2   2
3             2      Qz3   3
3             2      Qz4   2

Assume all columns are as factors except for quiz$Marks.

I would like to create another factor column quiz$zip using something like this:

if ((quiz$Group==1) & (quiz$Test=='Qz2'| quiz$Test == 'Qz4'))
     {quiz$zip<-3}
else if ((quiz$Group==1) & (quiz$Test=='Qz1'| quiz$Test == 'Qz3'))
     {quiz$zip<-2}
else
     {quiz$zip<-1}

I cannot make it work, and other than the fact that it is late at night, I cannot find a reason.

Your help and enlightenment is much appreciated!

Aucun commentaire:

Enregistrer un commentaire