生成一个新变量,如果满足任何条件,而无需在 R 中列出所有变量。

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

Generating a new variable if any of the conditions are met without list all variables in R

问题

我想生成一个名为outcome的变量,如果以下数据集中的任何列具有任何形式的同意响应,将其分配为1,否则分配为0。但是,我不想在我的代码中列出所有变量。

我尝试了以下代码:

vars <- c("a1", "a2", "a3", "a4")
dat <- dat %>%
  mutate(outcome = case_when(if_any(vars, ~ .x == "consented now" | "consented later") ~ 1))

数据集

dat1 <- tibble(
  a1 = c("consented now", NA, NA, NA),
  a2 = c("", "Refused", NA, NA),
  a3 = c(NA, "consented now", NA, NA),
  a4 = c(NA, NA, NA, "consented later"))

希望这有所帮助。

英文:

I would like to generate a variable called outcome which assigns 1 if any of the columns in the dataset below have any form of consent response else assign 0. However, I do not want to list all variables in my code.
I have tried the following code;

vars&lt;-c(&quot;a1&quot;,&quot;a2&quot;,&quot;a3&quot;,&quot;a4&quot;)
dat&lt;-dat%&gt;% 
  mutate(outcome = case_when(if_any(vars, ~ .x == &quot;consented now&quot;|
                                            &quot;consented later&quot;) ~ 1))

dataset

dat1 &lt;- tibble(
  a1 = c(&quot;consented now&quot;, NA, NA, NA),
  a2= c(&quot;&quot;, &quot;Refused&quot;, NA, NA),
  a3= c(NA, &quot;consented now&quot;, NA, NA),
  a4= c(NA, NA, NA, &quot;consented later&quot;))

答案1

得分: 1

不需要使用 `case_when`,可以使用 `if_any` 和 `grepl` 来实现:
```r
dat1 %>%
  mutate(outcome = +if_any(a1:a4, ~ grepl("consented", .x)))

输出:

# A tibble: 4 × 5
#  a1            a2        a3            a4              outcome
#  <chr>         <chr>     <chr>         <chr>             <int>
#1 consented now ""        NA            NA                    1
#2 NA            "Refused" consented now NA                    1
#3 NA             NA       NA            NA                    0
#4 NA             NA       NA            consented later       1
英文:

You don't need case_when, with if_any and grepl:

dat1 %&gt;% 
  mutate(outcome = +if_any(a1:a4, ~ grepl(&quot;consented&quot;, .x)))

output

# A tibble: 4 &#215; 5
#  a1            a2        a3            a4              outcome
#  &lt;chr&gt;         &lt;chr&gt;     &lt;chr&gt;         &lt;chr&gt;             &lt;int&gt;
#1 consented now &quot;&quot;        NA            NA                    1
#2 NA            &quot;Refused&quot; consented now NA                    1
#3 NA             NA       NA            NA                    0
#4 NA             NA       NA            consented later       1

答案2

得分: 1

使用pastedo.callgrepl基本变体可能如下所示:

dat1$outcome &lt;- +grepl("consented", do.call(paste, dat1))

dat1
#             a1      a2            a3              a4 outcome
#1 consented now                  &lt;NA&gt;            &lt;NA&gt;       1
#2          &lt;NA&gt; Refused consented now            &lt;NA&gt;       1
#3          &lt;NA&gt;    &lt;NA&gt;          &lt;NA&gt;            &lt;NA&gt;       0
#4          &lt;NA&gt;    &lt;NA&gt;          &lt;NA&gt; consented later       1

或者使用rowSumssapply

dat1$outcome &lt;- +(rowSums(sapply(dat1, grepl, pattern="consented")) > 0)
英文:

A base variant using paste with do.call and grepl might be:

dat1$outcome &lt;- +grepl(&quot;consented&quot;, do.call(paste, dat1))

dat1
#             a1      a2            a3              a4 outcome
#1 consented now                  &lt;NA&gt;            &lt;NA&gt;       1
#2          &lt;NA&gt; Refused consented now            &lt;NA&gt;       1
#3          &lt;NA&gt;    &lt;NA&gt;          &lt;NA&gt;            &lt;NA&gt;       0
#4          &lt;NA&gt;    &lt;NA&gt;          &lt;NA&gt; consented later       1

Or using rowSums and sapply.

dat1$outcome &lt;- +(rowSums(sapply(dat1, grepl, pattern=&quot;consented&quot;)) &gt; 0)

huangapple
  • 本文由 发表于 2023年2月13日 22:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437553.html
匿名

发表评论

匿名网友

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

确定