Why this happen?

Why do these 2 ways of making changes to a base produce different bases when it should be the same result?

This way I put all the changes in the same mutate

df1<- 
  df%>%
  mutate(contactos_prom= contactos_sistema/10,
         contactos_prom1= case_when(between(contactos_prom, 0, 7) ~ "Hasta 7",
                                    between(contactos_prom, 7.1, max(aticamama$contactos_prom)) ~ "7,1 y mas"),
         contactos_prom2= case_when(between(contactos_prom, 0, 3.5) ~ "Hasta 3,5",
                                    between(contactos_prom, 3.51, 7) ~ "3,51 a 7",
                                    between(contactos_prom, 7.1, max(aticamama$contactos_prom)) ~ "Mas de 7"))

Warning message:
There were 4 warnings in `mutate()`.
The first warning was:
ℹ In argument: `contactos_prom1 = case_when(...)`.
Caused by warning:
! Unknown or uninitialised column: `contactos_prom`.
ℹ Run dplyr::last_dplyr_warnings() to see the 3 remaining warnings. 

In this way I put the changes in mutate separately and no warning appears

df<- 
  df%>%
  mutate(contactos_prom= contactos_sistema/10)

df<-
df%>%
  mutate(contactos_prom1= case_when(between(contactos_prom, 0, 7) ~ "Hasta 7",
                                    between(contactos_prom, 7.1, max(aticamama$contactos_prom)) ~ "7,1 y mas"),
         contactos_prom2= case_when(between(contactos_prom, 0, 3.5) ~ "Hasta 3,5",
                                    between(contactos_prom, 3.51, 7) ~ "3,51 a 7",
                                    between(contactos_prom, 7.1, max(aticamama$contactos_prom)) ~ "Mas de 7"))

If I check there are not the same df, why?

all.equal(df, df1) 
[1] "Component “contactos_prom1”: 'is.NA' value mismatch: 132 in current 0 in target"
[2] "Component “contactos_prom2”: 'is.NA' value mismatch: 132 in current 0 in target"

I cannot reproduce the warning. The following code runs for me with no warning. Does it work for you?

library(dplyr)
DF <- data.frame(A = c(1,2,2,1))
DF |> mutate(B = A * 2,
             C = case_when(
               between(B, 1, 2) ~ "small",
               between(B, 3, 4) ~ "large"
             ))

Are there NA values in aticamama$contactos_prom?

What if you just ran it with max(contactos_prom) instead of max(aticamama$contactos_prom)?

This topic was automatically closed 21 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.