jeudi 21 janvier 2016

Explanation for If statement in R

There are several questions about the if statement on this forum. I tried the suggestions but the code produced the same error each time. Here is a description of what the code should do:

  • Create a new column (Efficiency) in an existing dataframe (file)
  • Populate Efficiency column based on values from an existing column (Duration)

I created the following code:

file$Efficiency<-if(file$Duration <= 5) {"Above Par"} else
{if(file$Duration > 5 & file$Duration <= 15) {"Par"} else
{if(file$Duration > 15 & file$Duration <= 30) {"Below Par"} else
{"Unacceptable"}}}

I get the following warning

Warning message:
In if (file$Duration <= 5) { :
the condition has length > 1 and only the first element will be used

It is mentioned in several posts that if statement takes one value and haven't accepted that as an explanation of what I see. If the statement is true then I wonder what is the use of the else function.

Aucun commentaire:

Enregistrer un commentaire