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