I am using ACS data to try to create a housing cost burden variable from two different variables, OCPIP and GRPIP.
I want to set those that report 0 as missing and any value greater than 30 as "Yes", and any value 1-30 as "No". Here is my dplyr
/Tidyverse
code:
df <- df %>%
mutate(cost_burdened = if_else(OCPIP == 0 | GRPIP == 0, NA_character_,
if_else(OCPIP < 31 | GRPIP < 31, "No", "Yes")))
I feel like I have it set up right from the way I am reading it which is if X or Y = 0, then they are missing and if X or Y is greater than 31 then they are "Yes". If I created two separate variables using the exact if_else
set up for one of them (so only GRPIP or OCPIP), the code works.
But when I add the |
it is not working. It is setting everything as missing. When I've tried different configurations including case_when
it is still not working.
CodePudding user response:
I don't know what could be causing your problems. See the example below. Everything works great here.
library(tidyverse)
set.seed(1111)
n=500
df = tibble(
OCPIP = sample(0:40, n, replace = TRUE),
GRPIP = sample(0:40, n, replace = TRUE),
) %>% mutate(cost_burdened = if_else(OCPIP == 0 | GRPIP == 0, NA_character_,
if_else(OCPIP < 31 | GRPIP < 31, "No", "Yes")))
output
# A tibble: 500 x 3
OCPIP GRPIP cost_burdened
<int> <int> <chr>
1 25 27 No
2 35 3 No
3 5 18 No
4 37 32 Yes
5 25 39 No
6 30 11 No
7 38 27 No
8 0 1 NA
9 12 19 No
10 23 16 No
# ... with 490 more rows
CodePudding user response:
Here's a case_when()
implementation:
library(tidyverse)
set.set(191)
cost_burdened <- tibble(
OCPIP = c(0, 12, 31, 35, 47),
GRPIP = c(31, 32, 15, 0, 47)
)
cost_burdened %>%
mutate(cost_burdened = case_when(
(OCPIP == 0) | (GRPIP == 0) ~ NA_character_,
(OCPIP < 31) | (GRPIP < 31) ~ "No",
TRUE ~ "Yes")
)