Search code examples
rsyntaxbinaryoperators

Correct operator usage


I'm using IPUMS CPS data and created a binary variable for disability from the six different variables within IPUMS for disability (https://cps.ipums.org/cps-action/variables/group/core_dis)

bias <- bias %>%
  mutate(DISABLED= if_else(.$DIFFCARE ==2|
                           .$DIFFMOB == 2 |
                           .$DIFFEYE ==2 |
                           .$DIFFREM == 2 |
                           .$DIFFPHYS == 2|
                           .$DIFFMOB == 2 |
                           .$DIFFCARE ==2 |
                           .$DIFFANY == 2|
                           .$DIFFHEAR == 2,1, 0))

If the respondent replied yes to any of the various disabilities, they were categorized under the new "Disabled" variable above with a "1". For not disabled, I'm looking for survey respondents to have selected "no disability" among the six variables. I believe the DISABLED code above, when coded as "0," may signify they only had categorized as not having a disability in one or more of the disability categories; however, I need them to be not disabled in all the categories.

So, I created this variable below, but I'm not sure this captures what I want.

bias <- bias %>%
  mutate(NOTDISABLED= if_else(.$DIFFCARE == 1|
                              .$DIFFMOB == 1 |
                              .$DIFFEYE == 1 |
                              .$DIFFREM == 1 |
                              .$DIFFPHYS == 1|
                              .$DIFFMOB == 1 |
                              .$DIFFCARE == 1 |
                              .$DIFFANY == 1|
                              .$DIFFHEAR == 1, 1, 0))

Please advise. Thank you again for reading!


Solution

  • As Sirius said, the key is using &:

    bias <- bias %>%
      mutate(NOTDISABLED= if_else(DIFFCARE == 1|
                                  DIFFMOB == 1 |
                                  DIFFEYE == 1 |
                                  DIFFREM == 1 |
                                  DIFFPHYS == 1|
                                  DIFFMOB == 1 |
                                  DIFFCARE == 1 |
                                  DIFFANY == 1|
                                  DIFFHEAR == 1, 1, 0),
            DISABLED= if_else(DIFFCARE == 1 &
                                  DIFFMOB == 1 &
                                  DIFFEYE == 1 &
                                  DIFFREM == 1 &
                                  DIFFPHYS == 1 &
                                  DIFFMOB == 1 &
                                  DIFFCARE == 1 &
                                  DIFFANY == 1 &
                                  DIFFHEAR == 1, 1, 0))
    

    Code simplified (removed .$s, they are unnecessary). See the documentation for more information on things like & and | (Boolean operators)