lundi 5 octobre 2015

r data.frame create a conditional column

I have data as below. I want a new column called accuracy_level. How can I get it done? I tried if but it didn't work well.

If the

  • accuracy_percentage is within +/-10% then accuracy_level will be "Good"
  • accuracy_percentage is within +/-30% and outside +/- 10% then accuracy_level will be "Bad"
  • accuracy_percentage is outside +/-30% then accuracy_level will be "Worst"

Here is my code:

actuals=seq(0,10,0.1)
forecast=seq(10,0,-0.1)
data1=data.frame(actuals,forecast)
data1$diff=data1$actuals-data1$forecast
data1$accuracy_percentage=(data1$diff/data1$actuals)*100
if((data1$accuracy_percentage < 10)&(data1$accuracy_percentage > -10),data1$accuracy_level="good",)

Aucun commentaire:

Enregistrer un commentaire