I have a problem when creating a variable based on 2 variables.
One variable is base$a8 of class numeric and the other one is base$a12 which are all numbers except some observations that contain "NC" and therefore the variable is of class character.
df<- base %>%
mutate(ocup_hor= case_when(a8==1 & a12>45 ~ "test1",
a8==1 & a12 %in% c(35:45) ~ "test2",
a8==1 & a12<35 ~ "test3",
a8==2 ~ "test4",
TRUE ~ "error"))
I apply this code to create it but for some reason there are 2 observations that instead of being test3 are coded as "error". I don't understand why.
df %>%
select(ocup_hor, a8,a12) %>%
filter(ocup_hor == "error")
# A tibble: 2 × 3
ocup_hor a8 a12
<chr> <dbl> <chr>
1 error 1 4
2 error 1 4
If a8 is 1 and a12 is 4, shouldn't it be recoded as test3?
I also make this code and surprisingly I get FALSE.
df %>%
select(ocup_hor, a8,a12) %>%
filter(ocup_hor == "error") %>%
mutate(a8==1 & a12<35)
# A tibble: 2 × 4
ocup_hor a8 a12 `a8 == 1 & a12 < 35`
<chr> <dbl> <chr> <lgl>
1 error 1 4 FALSE
2 error 1 4 FALSE
But if instead of a12<35 I write a12<40 I get the correct result.
df %>%
select(ocup_hor, a8,a12) %>%
filter(ocup_hor == "error") %>%
mutate(a8==1 & a12<40)
# A tibble: 2 × 4
ocup_hor a8 a12 `a8 == 1 & a12 < 40`
<chr> <dbl> <chr> <lgl>
1 error 1 4 TRUE
2 error 1 4 TRUE
Does anyone know why?