dimanche 28 avril 2019

Performing multiple actions in an IF statement

I want to change the values of multiple variables, based on the condition of another variable

Something like:

df <- iris
df$index <- row.names(df)

if(df$index == 10){
  df$Species <- "test";
  df$Sepal.Length <- 100
}

So if the index value is 10, then I want to change Species to "test" AND change sepal.length to 100.

Instead I get a warning:

Warning message:
In if (df$index == 10) { :
  the condition has length > 1 and only the first element will be used

And the variables remain unchanged.

Aucun commentaire:

Enregistrer un commentaire