jeudi 25 juin 2020

Transforming a variable using the if- else if function

I have a data set that is that I want to calculate z scores by their year.

Example:

  Year  Score
   1999   120
   1999   132
   1998   120
   1997   132
   2000   120
   2002   132
   1998   160
   1997   142
   ....etc

What I want is:

  Year  Score  Z-Score
   1999   120  1.2
   1999   132  .01
   1998   120  -.6
   1997   132  1.1
   2000   120  -.6
   2002   132  0.5
   1998   160  2.1
   1997   142  .01

I have used the following code:

DF$ZScore<-if (DR$Year== 1997){
((DF$Score-220)/20)
} else if ((DR$Year== 1998){
((DF$Score-222)/19)
}...
}else{
((DF$Score-219)/21)
}

This is not working and I cannot figure out why. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire