英文:
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)。
你尝试了以下代码:
df1 <- df %>%
group_by(ID, conception, Dx) %>%
mutate(copy_Dx = if_else(n_distinct(date) > 1, Dx, "0")) %>%
ungroup()
但是你遇到了以下错误:
由if_else()
引起的错误:
true
的大小必须为1,而不是2。
你已经用ifelse替换了if_else,并且它有效,但我想知道是否有一种方法可以使它与if_else一起工作。
如果你想使用if_else
,你可以使用以下方式来解决错误:
df1 <- df %>%
group_by(ID, conception, Dx) %>%
mutate(copy_Dx = if_else(n_distinct(date_Dx) > 1, Dx, "0")) %>%
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:
df1 <- df%>%
group_by(ID, conception, Dx)%>%
mutate(copy_Dx = if_else(n_distinct(date)>1, Dx,"0"))%>%
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
:
df %>%
group_by(ID, conception, Dx) %>%
mutate(copy_Dx = if (n_distinct(date_Dx) > 1) Dx else "0") %>%
ungroup()
# # A tibble: 4 × 5
# ID conception date_Dx Dx copy_Dx
# <int> <chr> <chr> <chr> <chr>
# 1 1 2005-11-14 2008-10-01 xx xx
# 2 1 2005-11-14 2008-04-08 yy 0
# 3 1 2005-11-14 2008-10-05 xx xx
# 4 1 2005-11-14 2008-04-08 zz 0
英文:
Use if
instead of if_else
:
df %>%
group_by(ID, conception, Dx) %>%
mutate(copy_Dx = if (n_distinct(date_Dx) > 1) Dx else "0") %>%
ungroup()
# # A tibble: 4 × 5
# ID conception date_Dx Dx copy_Dx
# <int> <chr> <chr> <chr> <chr>
# 1 1 2005-11-14 2008-10-01 xx xx
# 2 1 2005-11-14 2008-04-08 yy 0
# 3 1 2005-11-14 2008-10-05 xx xx
# 4 1 2005-11-14 2008-04-08 zz 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论