如何基于多个参数传递不同行的代码在R中?

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

How to pass different lines of code based on multiple arguments in R?

问题

由于编辑太多而开启新会话。

我有一个数据框,看起来像这样:

  1. df <- data.frame(ID = c("A","A","A","B","B","B","C","C","C"),
  2. value = c(1,2,3,1,2,3,1,2,3))

我想要使用条件语句 if else 传递类似以下内容,但它不起作用,因为条件的长度大于1

  1. if (df$ID == "A" | df$ID == "B") {
  2. df$nat[df$value %in% c(1,2)] <- 1
  3. df$hrv[df$value %in% c(3)] <- 1
  4. } else {
  5. df$nat[df$value %in% c(1)] <- 1
  6. df$hrv[df$value %in% c(2,3)] <- 1
  7. }

我知道我可以拆分数据集并分别进行计算,然后再将它们绑定在一起,但是否有更简单的方法,类似我上面提到的?

所以最终我想要的是这样的:

  1. df <- data.frame(ID = c("A","A","A","B","B","B","C","C","C"),
  2. value = c(1,2,3,1,2,3,1,2,3),
  3. nat = c(1,1,NA,1,1,NA,1,NA,NA),
  4. hrv = c(NA,NA,1,NA,NA,1,NA,1,1))
英文:

Opening up a new session due to too many edits.

I have a dataframe that looks like this:

  1. df &lt;- data.frame(ID = c(&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;C&quot;,&quot;C&quot;,&quot;C&quot;),
  2. value = c(1,2,3,1,2,3,1,2,3))

And I would like to pass something like this using an if else statement, but it does not work because the condition has length &gt;1:

  1. if (df$ID ==&quot;A&quot;| df$ID==&quot;B&quot;){
  2. df$nat[df$value %in% c(1,2)] &lt;- 1
  3. df$hrv[df$value %in% c(3)] &lt;- 1}
  4. else {
  5. df$nat[df$value %in% c(1)] &lt;- 1
  6. df$hrv[df$value %in% c(2,3)] &lt;- 1}

I know I could split the dataset and do seperate calcululations and bind it together again, but is there a simpler approach, something like I suggested above?

So I would end up with something like this:

  1. df &lt;- data.frame(ID = c(&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;C&quot;,&quot;C&quot;,&quot;C&quot;),
  2. value = c(1,2,3,1,2,3,1,2,3),
  3. nat = c(1,1,NA,1,1,NA,1,NA,NA),
  4. hrv = c(NA,NA,1,NA,NA,1,NA,1,1))

答案1

得分: 2

你可以使用ifelseif_elsecase_when语句的变体。以下是使用dplyr中的case_when的示例。

  1. library(dplyr)
  2. df %>%
  3. mutate(isAB = ID %in% c("A", "B"),
  4. nat = case_when((isAB & value %in% c(1, 2))|(!isAB & value %in% 1) ~ 1),
  5. hrv = case_when((isAB & value %in% 3)|(!isAB & value %in% c(2, 3)) ~ 1))
  6. # ID value isAB nat hrv
  7. #1 A 1 TRUE 1 NA
  8. #2 A 2 TRUE 1 NA
  9. #3 A 3 TRUE NA 1
  10. #4 B 1 TRUE 1 NA
  11. #5 B 2 TRUE 1 NA
  12. #6 B 3 TRUE NA 1
  13. #7 C 1 FALSE 1 NA
  14. #8 C 2 FALSE NA 1
  15. #9 C 3 FALSE NA 1

isAB是一个临时列,用于避免多次执行ID %in% c("A", "B")。如果不需要,你可以在最后删除它。

我们使用了OR (|) 来组合两个条件。

英文:

You can use variations of ifelse, if_else or case_when statements. Here is one using case_when in dplyr.

  1. library(dplyr)
  2. df %&gt;%
  3. mutate(isAB = ID %in% c(&quot;A&quot;, &quot;B&quot;),
  4. nat = case_when((isAB &amp; value %in% c(1, 2))|(!isAB &amp; value %in% 1) ~ 1),
  5. hrv = case_when((isAB &amp; value %in% 3)|(!isAB &amp; value %in% c(2, 3)) ~ 1))
  6. # ID value isAB nat hrv
  7. #1 A 1 TRUE 1 NA
  8. #2 A 2 TRUE 1 NA
  9. #3 A 3 TRUE NA 1
  10. #4 B 1 TRUE 1 NA
  11. #5 B 2 TRUE 1 NA
  12. #6 B 3 TRUE NA 1
  13. #7 C 1 FALSE 1 NA
  14. #8 C 2 FALSE NA 1
  15. #9 C 3 FALSE NA 1

isAB is a temporary column created to avoid doing ID %in% c(&quot;A&quot;, &quot;B&quot;) multiple times. You can drop it at the end if it is not needed.

We have combined two conditions using OR (|).

huangapple
  • 本文由 发表于 2023年6月27日 17:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563483.html
匿名

发表评论

匿名网友

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

确定