mercredi 29 novembre 2017

How to write R if statements and conditional variables?

I am trying to create a variable with conditions. For example, I want to create a new variable called SchoolLevel that equals 1 if the variable MATH is less than or equal to 1000. I want SchoolLevel to equal 2 if MATH is greater than 1000 or less than and equal to 2000. And I want SchoolLevel to equal 3 if MATH is greater than 2000.

I have tried looking on Google and tried to enter this:

if(MATH<=1000) SchooolLevel = 1

else if(1000<MATH & MATH<=2000) SchoolLevel = 2

But I get an error that says:

In if (MATH <= 1000) { :the condition has length > 1 and only the first element will be used}

Can anyone help me out? Thanks!

Aucun commentaire:

Enregistrer un commentaire