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

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

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

问题

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

我尝试了以下代码:

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

数据集

  1. dat1 <- tibble(
  2. a1 = c("consented now", NA, NA, NA),
  3. a2 = c("", "Refused", NA, NA),
  4. a3 = c(NA, "consented now", NA, NA),
  5. 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;

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

dataset

  1. dat1 &lt;- tibble(
  2. a1 = c(&quot;consented now&quot;, NA, NA, NA),
  3. a2= c(&quot;&quot;, &quot;Refused&quot;, NA, NA),
  4. a3= c(NA, &quot;consented now&quot;, NA, NA),
  5. a4= c(NA, NA, NA, &quot;consented later&quot;))

答案1

得分: 1

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

输出:

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

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

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

output

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

答案2

得分: 1

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

  1. dat1$outcome &lt;- +grepl("consented", do.call(paste, dat1))
  2. dat1
  3. # a1 a2 a3 a4 outcome
  4. #1 consented now &lt;NA&gt; &lt;NA&gt; 1
  5. #2 &lt;NA&gt; Refused consented now &lt;NA&gt; 1
  6. #3 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 0
  7. #4 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; consented later 1

或者使用rowSumssapply

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

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

  1. dat1$outcome &lt;- +grepl(&quot;consented&quot;, do.call(paste, dat1))
  2. dat1
  3. # a1 a2 a3 a4 outcome
  4. #1 consented now &lt;NA&gt; &lt;NA&gt; 1
  5. #2 &lt;NA&gt; Refused consented now &lt;NA&gt; 1
  6. #3 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; 0
  7. #4 &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; consented later 1

Or using rowSums and sapply.

  1. 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:

确定