如何在R语言中按Mother_ID计算妊娠间隔?
在Base R中计算母亲的妊娠间隔(含多胎、流产/死胎场景)
需求说明
基于Mother_ID计算每位母亲的妊娠间隔,需覆盖以下场景:
- 一位母亲多次生育(2次及以上)
- 妊娠结局为流产(Miscarriage)或死胎(Stillbirth)的情况
- 多胞胎(同一妊娠对应多条记录)
要求使用Base R实现,最终结果按Mother_ID和Birth_date排序,新增Inter_preg_inter (days)列展示间隔天数。
原始数据集
| Mother_ID | Last_mentrual_period | Birth_date | Nr_fetuses | Preg_outcome | Gestational_age | Child_ID |
|---|---|---|---|---|---|---|
| 1 | 1996-04-15 | 1996-12-08 | 1 | Livebirth | 237 | C1 |
| 2 | 2018-06-01 | 2019-02-18 | 1 | Livebirth | 262 | C2 |
| 3 | 2002-08-23 | 2003-05-07 | 1 | Livebirth | 257 | C3 |
| 3 | 1998-04-22 | 1999-01-15 | 2 | LiveBirth | 268 | C4 |
| 3 | 1998-04-22 | 1999-01-15 | 2 | Livebirth | 268 | C5 |
| 3 | 1992-02-21 | 1992-11-22 | 1 | Livebirth | 275 | C6 |
| 4 | 2006-02-28 | 2006-11-18 | 1 | Livebirth | 263 | C7 |
| 4 | 2003-01-31 | 2003-11-12 | 1 | Livebirth | 285 | C8 |
| 4 | 2005-01-04 | 2005-03-18 | 1 | Miscarriage | 73 | |
| 5 | 2009-04-08 | 2009-06-06 | 1 | Miscarriage | 59 | |
| 5 | 2009-08-01 | 2010-05-02 | 1 | Stillbirth | 274 | C9 |
| 6 | 1992-02-02 | 1992-09-05 | 1 | Stillbirth | 216 | |
| 6 | 1995-02-21 | 1995-11-13 | 1 | Livebirth | 265 | C10 |
| 6 | 1990-02-08 | 1990-11-07 | 1 | Livebirth | 272 | C11 |
妊娠间隔计算方式
两种等价计算方式,结果一致:
- 方式1:
当前妊娠的Last_mentrual_period - 上一次妊娠的Birth_date - 方式2:
当前妊娠的Birth_date - 上一次妊娠的Birth_date - 当前妊娠的Gestational_age
Base R实现代码
# 1. 准备数据集,转换日期格式 df <- data.frame( Mother_ID = c(1,2,3,3,3,3,4,4,4,5,5,6,6,6), Last_mentrual_period = as.Date(c("1996-04-15","2018-06-01","2002-08-23","1998-04-22","1998-04-22","1992-02-21","2006-02-28","2003-01-31","2005-01-04","2009-04-08","2009-08-01","1992-02-02","1995-02-21","1990-02-08")), Birth_date = as.Date(c("1996-12-08","2019-02-18","2003-05-07","1999-01-15","1999-01-15","1992-11-22","2006-11-18","2003-11-12","2005-03-18","2009-06-06","2010-05-02","1992-09-05","1995-11-13","1990-11-07")), Nr_fetuses = c(1,1,1,2,2,1,1,1,1,1,1,1,1,1), Preg_outcome = c("Livebirth","Livebirth","Livebirth","LiveBirth","Livebirth","Livebirth","Livebirth","Livebirth","Miscarriage","Miscarriage","Stillbirth","Stillbirth","Livebirth","Livebirth"), Gestational_age = c(237,262,257,268,268,275,263,285,73,59,274,216,265,272), Child_ID = c("C1","C2","C3","C4","C5","C6","C7","C8","","","C9","","C10","C11"), stringsAsFactors = FALSE ) # 2. 按Mother_ID和Birth_date排序 df_sorted <- df[order(df$Mother_ID, df$Birth_date),] # 3. 提取每个母亲的唯一妊娠记录,计算间隔 preg_unique <- unique(df_sorted[,c("Mother_ID","Birth_date","Last_mentrual_period")]) preg_unique <- preg_unique[order(preg_unique$Mother_ID, preg_unique$Birth_date),] preg_unique$inter <- with(preg_unique, ave(Last_mentrual_period, Mother_ID, FUN = function(x) c(0, diff(x)))) # 4. 将间隔匹配回原始排序后的数据集 df_final <- merge(df_sorted, preg_unique[,c("Mother_ID","Birth_date","inter")], by = c("Mother_ID","Birth_date"), all.x = TRUE) # 5. 添加Line列,调整列顺序 df_final$Line <- 1:nrow(df_final) df_final <- df_final[,c("Line","Mother_ID","Last_mentrual_period","Birth_date","Nr_fetuses","Preg_outcome","Gestational_age","Child_ID","inter")] colnames(df_final)[9] <- "Inter_preg_inter (days)" # 查看结果 print(df_final, row.names = FALSE)
期望输出结果
| Line | Mother_ID | Last_mentrual_period | Birth_date | Nr_fetuses | Preg_outcome | Gestational_age | Child_ID | Inter_preg_inter (days) |
|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 1996-04-15 | 1996-12-08 | 1 | Livebirth | 237 | C1 | 0 |
| 2 | 2 | 2018-06-01 | 2019-02-18 | 1 | Livebirth | 262 | C2 | 0 |
| 3 | 3 | 1992-02-21 | 1992-11-22 | 1 | Livebirth | 275 | C6 | 0 |
| 4 | 3 | 1998-04-22 | 1999-01-15 | 2 | LiveBirth | 268 | C4 | 1977 |
| 5 | 3 | 1998-04-22 | 1999-01-15 | 2 | Livebirth | 268 | C5 | 1977 |
| 6 | 3 | 2002-08-23 | 2003-05-07 | 1 | Livebirth | 257 | C3 | 1316 |
| 7 | 4 | 2003-01-31 | 2003-11-12 | 1 | Livebirth | 285 | C8 | 0 |
| 8 | 4 | 2005-01-04 | 2005-03-18 | 1 | Miscarriage | 73 | 419 | |
| 9 | 4 | 2006-02-28 | 2006-11-18 | 1 | Livebirth | 263 | C7 | 347 |
| 10 | 5 | 2009-04-08 | 2009-06-06 | 1 | Miscarriage | 59 | 0 | |
| 11 | 5 | 2009-08-01 | 2010-05-02 | 1 | Stillbirth | 274 | C9 | 56 |
| 12 | 6 | 1990-02-08 | 1990-11-07 | 1 | Livebirth | 272 | C11 | 0 |
| 13 | 6 | 1992-02-02 | 1992-09-05 | 1 | Stillbirth | 216 | 452 | |
| 14 | 6 | 1995-02-21 | 1995-11-13 | 1 | Livebirth | 265 | C10 | 899 |
内容的提问来源于stack exchange,提问作者user21037671
相关产品推荐
相关产品推荐

