英文:
Correct operator usage
问题
我使用IPUMS CPS数据创建了一个残疾的二进制变量,该变量基于IPUMS中六个不同的残疾变量(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))
如果受访者对任何一种残疾答复是"是",则他们将被归类到上面新的"Disabled"变量中,值为"1"。对于不残疾的情况,我希望调查受访者在这六个变量中选择了"无残疾"。我认为上面的DISABLED代码,当编码为"0"时,可能表示他们只在一个或多个残疾类别中被归类为没有残疾;但是,我需要他们在所有类别中都没有残疾。
因此,我创建了下面的这个变量,但我不确定这是否符合我的要求。
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))
请给予建议。再次感谢您的阅读!
英文:
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!
答案1
得分: 0
正如Sirius所说,关键是使用&
:
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)
)
代码简化(删除了.$
,它们是不必要的)。有关&
和|
(布尔运算符)等更多信息,请参阅文档。
英文:
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)
答案2
得分: 0
你可以像这样避免多个ifelse
语句:
suppressPackageStartupMessages(library(tidyverse))
bias <- data.frame(id = 1:5,
DIFFCARE = c(0,0,0,0,0),
DIFFMOB = c(0,0,1,0,0),
DIFFEYE = c(0,1,0,0,0),
DIFFREM = c(0,0,0,0,1),
DIFFPHYS = c(0,0,0,0,0),
DIFFANY = c(0,0,0,0,0),
DIFFHEAR = c(0,0,1,0,0))
bias %>%
mutate(diffsum = rowSums(across(contains("DIFF"))),
Disabled = ifelse(diffsum == 0, "No", "Yes"))
#> id DIFFCARE DIFFMOB DIFFEYE DIFFREM DIFFPHYS DIFFANY DIFFHEAR diffsum
#> 1 1 0 0 0 0 0 0 0 0
#> 2 2 0 0 1 0 0 0 0 1
#> 3 3 0 1 0 0 0 0 1 2
#> 4 4 0 0 0 0 0 0 0 0
#> 5 5 0 0 0 1 0 0 0 1
#> Disabled
#> 1 No
#> 2 Yes
#> 3 Yes
#> 4 No
#> 5 Yes
Created on 2023-06-26 with reprex v2.0.2
英文:
You can avoid the multiple ifelse
statements like this:
suppressPackageStartupMessages(library(tidyverse))
bias <- data.frame(id = 1:5,
DIFFCARE = c(0,0,0,0,0),
DIFFMOB = c(0,0,1,0,0),
DIFFEYE = c(0,1,0,0,0),
DIFFREM = c(0,0,0,0,1),
DIFFPHYS = c(0,0,0,0,0),
DIFFANY = c(0,0,0,0,0),
DIFFHEAR = c(0,0,1,0,0))
bias %>%
mutate(diffsum = rowSums(across(contains("DIFF"))),
Disabled = ifelse(diffsum == 0, "No", "Yes"))
#> id DIFFCARE DIFFMOB DIFFEYE DIFFREM DIFFPHYS DIFFANY DIFFHEAR diffsum
#> 1 1 0 0 0 0 0 0 0 0
#> 2 2 0 0 1 0 0 0 0 1
#> 3 3 0 1 0 0 0 0 1 2
#> 4 4 0 0 0 0 0 0 0 0
#> 5 5 0 0 0 1 0 0 0 1
#> Disabled
#> 1 No
#> 2 Yes
#> 3 Yes
#> 4 No
#> 5 Yes
<sup>Created on 2023-06-26 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论