vendredi 10 mai 2019

If-else error statement `the condition has length > 1`in R?

I am writing a simple ifelse statement following another , where I'm trying to classify the index into three groups ( >= 0.8, 0.8 to -0.8, <= -0.8)

I keep getting an error:

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

index <- c(0.8,0.2,-0.2,0,-1,-1)
df <- data.frame(index)
df$indexclass <- NA


df$indexclass  <- if (df$index >= 0.8) {
  df$indexclass  <- "P"
} else if (df$index <= (-0.8)) {
  df$indexclass  <- "A"
} else { df$indexclass <- "S"}

Aucun commentaire:

Enregistrer un commentaire