You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言中按Mother_ID计算妊娠间隔?

在Base R中计算母亲的妊娠间隔(含多胎、流产/死胎场景)

需求说明

基于Mother_ID计算每位母亲的妊娠间隔,需覆盖以下场景:

  • 一位母亲多次生育(2次及以上)
  • 妊娠结局为流产(Miscarriage)或死胎(Stillbirth)的情况
  • 多胞胎(同一妊娠对应多条记录)
    要求使用Base R实现,最终结果按Mother_ID和Birth_date排序,新增Inter_preg_inter (days)列展示间隔天数。

原始数据集

Mother_IDLast_mentrual_periodBirth_dateNr_fetusesPreg_outcomeGestational_ageChild_ID
11996-04-151996-12-081Livebirth237C1
22018-06-012019-02-181Livebirth262C2
32002-08-232003-05-071Livebirth257C3
31998-04-221999-01-152LiveBirth268C4
31998-04-221999-01-152Livebirth268C5
31992-02-211992-11-221Livebirth275C6
42006-02-282006-11-181Livebirth263C7
42003-01-312003-11-121Livebirth285C8
42005-01-042005-03-181Miscarriage73
52009-04-082009-06-061Miscarriage59
52009-08-012010-05-021Stillbirth274C9
61992-02-021992-09-051Stillbirth216
61995-02-211995-11-131Livebirth265C10
61990-02-081990-11-071Livebirth272C11

妊娠间隔计算方式

两种等价计算方式,结果一致:

  • 方式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)

期望输出结果

LineMother_IDLast_mentrual_periodBirth_dateNr_fetusesPreg_outcomeGestational_ageChild_IDInter_preg_inter (days)
111996-04-151996-12-081Livebirth237C10
222018-06-012019-02-181Livebirth262C20
331992-02-211992-11-221Livebirth275C60
431998-04-221999-01-152LiveBirth268C41977
531998-04-221999-01-152Livebirth268C51977
632002-08-232003-05-071Livebirth257C31316
742003-01-312003-11-121Livebirth285C80
842005-01-042005-03-181Miscarriage73419
942006-02-282006-11-181Livebirth263C7347
1052009-04-082009-06-061Miscarriage590
1152009-08-012010-05-021Stillbirth274C956
1261990-02-081990-11-071Livebirth272C110
1361992-02-021992-09-051Stillbirth216452
1461995-02-211995-11-131Livebirth265C10899

内容的提问来源于stack exchange,提问作者user21037671

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:40:17