Jorge Hernández
Jorge Hernández

Reputation: 662

Why dplyr case_when doesnt work correctly?

I am working on a data frame and I want to generate a new variable conditionally based on values, but those values greater than 1 are supposed to say "Sin sentido", however, this is not the case and it still appears "En desarrollo", Does anyone know why this happens? Is there an error in the syntax of my code? Hope you can help me The code is the following:

ejercicio <- data.frame(
  dato1 = c(0,0.1,0.5,1,1.1)
)


ejercicio <- ejercicio |> 
  mutate(Estatus = case_when(
    dato1 == 1 ~ "Completado",
    dato1 == 0 ~ "Sin empezar",
    dato1 > 0 | dato1 < 1 ~ "En desarrollo",
    dato1 > 1 ~ "Sin sentido"
  ))

Upvotes: 1

Views: 1064

Answers (1)

akrun
akrun

Reputation: 887028

The reason is that the third condition already meets for all the rows

 with(ejercicio, dato1 > 0 | dato1 < 1)
[1] TRUE TRUE TRUE TRUE TRUE

Thus, the last condition is not evaluated. Probably, we could change the sequence of expression i.e. the last should go above the third condition

ejercicio |> 
  mutate(Estatus = case_when(
    dato1 == 1 ~ "Completado",
    dato1 == 0 ~ "Sin empezar",
    dato1 > 1 ~ "Sin sentido",
    dato1 > 0 | dato1 < 1 ~ "En desarrollo"
    
  ))

-output

dato1       Estatus
1   0.0   Sin empezar
2   0.1 En desarrollo
3   0.5 En desarrollo
4   1.0    Completado
5   1.1   Sin sentido

NOTE: These are floats, so using == should be careful i.e. it may not be exactly equal unless precision is taken care of

Upvotes: 2

Related Questions