正确的运算符使用

huangapple go评论74阅读模式
英文:

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 &lt;- bias %&gt;%
  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 &lt;- bias %&gt;%
  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所说,关键是使用&amp;

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 &amp;
      DIFFMOB == 1 &amp;
      DIFFEYE == 1 &amp;
      DIFFREM == 1 &amp;
      DIFFPHYS == 1 &amp;
      DIFFMOB == 1 &amp;
      DIFFCARE == 1 &amp;
      DIFFANY == 1 &amp;
      DIFFHEAR == 1, 1, 0)
  )

代码简化(删除了.$,它们是不必要的)。有关&amp;|(布尔运算符)等更多信息,请参阅文档

英文:

As Sirius said, the key is using &amp;:

bias &lt;- bias %&gt;%
  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 &amp;
                              DIFFMOB == 1 &amp;
                              DIFFEYE == 1 &amp;
                              DIFFREM == 1 &amp;
                              DIFFPHYS == 1 &amp;
                              DIFFMOB == 1 &amp;
                              DIFFCARE == 1 &amp;
                              DIFFANY == 1 &amp;
                              DIFFHEAR == 1, 1, 0))

Code simplified (removed .$s, they are unnecessary). See the documentation for more information on things like &amp; 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 &lt;- 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 %&gt;%
  mutate(diffsum = rowSums(across(contains(&quot;DIFF&quot;))),
         Disabled = ifelse(diffsum == 0, &quot;No&quot;, &quot;Yes&quot;))
#&gt;   id DIFFCARE DIFFMOB DIFFEYE DIFFREM DIFFPHYS DIFFANY DIFFHEAR diffsum
#&gt; 1  1        0       0       0       0        0       0        0       0
#&gt; 2  2        0       0       1       0        0       0        0       1
#&gt; 3  3        0       1       0       0        0       0        1       2
#&gt; 4  4        0       0       0       0        0       0        0       0
#&gt; 5  5        0       0       0       1        0       0        0       1
#&gt;   Disabled
#&gt; 1       No
#&gt; 2      Yes
#&gt; 3      Yes
#&gt; 4       No
#&gt; 5      Yes

<sup>Created on 2023-06-26 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月26日 05:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定