创建一个基于相同疾病和相同个体的不同日期的新列。

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

Create a new column based on different dates for same disease for same individual

问题

ID conception date_Dx Dx copy_Dx
1 2005-11-14 2008-10-01 xx xx
1 2005-11-14 2008-04-08 yy 0
1 2005-11-14 2008-10-05 xx xx
1 2005-11-14 2008-04-08 zz 0

你想要创建一个新的列(copy_Dx),如果有人在不同的date_Dx上有相同的Dx,则将Dx复制,否则复制的值为0。基本上,你想这样定义copy_Dx:同一种疾病(Dx)具有多个相同的日期(date_Dx)。

你尝试了以下代码:

  1. df1 <- df %>%
  2. group_by(ID, conception, Dx) %>%
  3. mutate(copy_Dx = if_else(n_distinct(date) > 1, Dx, "0")) %>%
  4. ungroup()

但是你遇到了以下错误:

if_else()引起的错误:
true的大小必须为1,而不是2。

你已经用ifelse替换了if_else,并且它有效,但我想知道是否有一种方法可以使它与if_else一起工作。

如果你想使用if_else,你可以使用以下方式来解决错误:

  1. df1 <- df %>%
  2. group_by(ID, conception, Dx) %>%
  3. mutate(copy_Dx = if_else(n_distinct(date_Dx) > 1, Dx, "0")) %>%
  4. ungroup()

这样应该可以解决问题,因为n_distinct(date_Dx)将返回一个大小为1的逻辑向量,适用于if_else函数。

英文:
ID conception date_Dx Dx
1 2005-11-14 2008-10-01 xx
1 2005-11-14 2008-04-08 yy
1 2005-11-14 2008-10-05 xx
1 2005-11-14 2008-04-08 zz

I am trying to create a new column (copy_Dx) whereby Dx is copied if someone has the same Dx on different date_Dx or else the value copied is 0. Basically, I am trying to define copy_Dx this way: More than one date with the same code (Dx) indicating particular disease.

I tried something like this:

  1. df1 &lt;- df%&gt;%
  2. group_by(ID, conception, Dx)%&gt;%
  3. mutate(copy_Dx = if_else(n_distinct(date)&gt;1, Dx,&quot;0&quot;))%&gt;%
  4. ungroup()

But I get the following error :
Caused by error in if_else():
! true must have size 1, not size 2.

I replaced if_else with ifelse, and that works, but I am wondering if there is a way to make it work with if_else.

答案1

得分: 0

使用if替代if_else

  1. df %>%
  2. group_by(ID, conception, Dx) %>%
  3. mutate(copy_Dx = if (n_distinct(date_Dx) > 1) Dx else "0") %>%
  4. ungroup()
  5. # # A tibble: 4 &#215; 5
  6. # ID conception date_Dx Dx copy_Dx
  7. # &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  8. # 1 1 2005-11-14 2008-10-01 xx xx
  9. # 2 1 2005-11-14 2008-04-08 yy 0
  10. # 3 1 2005-11-14 2008-10-05 xx xx
  11. # 4 1 2005-11-14 2008-04-08 zz 0
英文:

Use if instead of if_else:

  1. df %&gt;%
  2. group_by(ID, conception, Dx) %&gt;%
  3. mutate(copy_Dx = if (n_distinct(date_Dx) &gt; 1) Dx else &quot;0&quot;) %&gt;%
  4. ungroup()
  5. # # A tibble: 4 &#215; 5
  6. # ID conception date_Dx Dx copy_Dx
  7. # &lt;int&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  8. # 1 1 2005-11-14 2008-10-01 xx xx
  9. # 2 1 2005-11-14 2008-04-08 yy 0
  10. # 3 1 2005-11-14 2008-10-05 xx xx
  11. # 4 1 2005-11-14 2008-04-08 zz 0

huangapple
  • 本文由 发表于 2023年7月27日 23:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781490.html
匿名

发表评论

匿名网友

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

确定