Condition has length > 1 and only the first element will be used

I would like to create a new column (PT) using base function in R for iris dataset which will display "Long" if the Petal Length is >=4 and "Short" if Petal Length is <4

I have written the below code, but it is giving me an error the condition has length > 1 and only the first element will be used. Can you please help me with this

iris$PT <- if(iris$Petal.Length>=3){"Long"} else {"Short"}

Thanks,
Vineet

Hi,

Welcome to the RStudio community!

When using an if-statement there is only one condition that can be true or false in order to run the if or not.

if(1 == 1){print("true")}
[1] "true"

If the output of the comparison generates more than one true or false, only the first one will be used to decide and you get the error

1 == 1:5
#> [1]  TRUE FALSE FALSE FALSE FALSE

if(1 == 1:5){print("true")}
#> Warning in if (1 == 1:5) {: the condition has length > 1 and only the first
#> element will be used
#> [1] "true"

Created on 2022-03-01 by the reprex package (v2.0.1)

In your case this is the same when you do

iris$Petal.Length >= 3

Which generates a lot of true / false outputs.

What you want to do is use the slightly different ifelse() function that will evaluate every comparison and then take the correct action

ifelse(2 > c(1,5,0,3), "larger", "smaller")
[1] "larger"  "smaller" "larger"  "smaller"

Or in your case

iris$PT <- ifelse(iris$Petal.Length >= 3, "Long", "Short")

Hope this helps,
PJ

Thank you so much PJ. That was really helpful

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.