library(ggplot2)
library(data.table)

Data loading and massaging

Load CGM data for every patient

setwd("~/Downloads/CGM/dexcom_20210301/")
cgm_files = grep("dexcom_clean_pop_", list.files("."), value = T)
cgm_data = rbindlist(lapply(cgm_files, fread))
cgm_data[, dexcom_date := as.Date(ts)]
cgm_data_days = cgm_data[,.(
  num_bg_recodings = .N 
  ), by = c('pop', 'record_id', 'sex', 'dob', 'insurance_type','dexcom_date')]

cgm_data_days[, wk := as.Date(cut(dexcom_date, "week"))]
t1d = cgm_data_days[, .(
  num_bg_recodings = sum(num_bg_recodings), 
  num_dates = uniqueN(dexcom_date),
  num_weeks = uniqueN(wk)
  ), by = c('pop', 'record_id', 'sex', 'dob', 'insurance_type')]

Add demographics

t1d[, dob := as.Date(dob, "%m/%d/%y")]
t1d[, age := floor(as.numeric((as.Date('2020-05-25') - dob))/365)]
t1d[, sex := as.factor(sex)]
t1d[, insurance_type := as.factor(insurance_type)]

Load Redcap data on pump use

redcap_4t_p = fread("~/Downloads/CGM/4TStudyTeamworkTarge_DATA_2021-03-06_1131.csv")
redcap_4t_p = redcap_4t_p[,.(pop = "4T", pump = max(fcoalesce(as.numeric(cgm_pump_use),0))), by='record_id']
redcap_pilot_p = fread("~/Downloads/CGM/CGMPilotStudyCGMInNe_DATA_2021-03-03_2025.csv")
redcap_pilot_p = redcap_pilot_p[,.(pop = "Pilot", pump = max(fcoalesce(as.numeric(pump_use),0))), by='record_id']
redcap_tips_p = fread("~/Downloads/CGM/CGMTips_DATA_2021-03-06_1131.csv")
redcap_tips_p = redcap_tips_p[,.(pop = "TIPS", pump = max(fcoalesce(as.numeric(pump_use),0))), by='record_id']
redcap_pump = rbind(redcap_4t_p, redcap_pilot_p, redcap_tips_p)
redcap_pump[,.(sum(pump), .N), by='pop']
##      pop V1   N
## 1:    4T 10  84
## 2: Pilot 67 136
## 3:  TIPS  5  76

Load Redcap data on contacts

redcap_4t = fread("~/Downloads/CGM/4TStudyTeamworkTarge_DATA_2021-03-06_1131.csv")
redcap_4t = redcap_4t[!is.na(rm_contact_date) | cgm_visit_type %in% c(1,2)]
redcap_4t = unique(redcap_4t[,.(
  pop="4T", record_id, 
  week_contact = as.Date(as.character(cut(fcoalesce(cgm_visitdt,rm_contact_date), "week"))),
  in_person_visit = cgm_visit_type == 1,
  telehealth_visit = cgm_visit_type == 2,
  remote_contact = !is.na(rm_contact_date) & rm_contact_week==1#,
  #cgm_pump_use = max(cgm_pump_use, na.rm=T)
  )])
redcap_4t = redcap_4t[!is.na(week_contact)]
# str(redcap_4t)
redcap_4t[,.(sum(in_person_visit, na.rm=T), sum(telehealth_visit, na.rm=T), sum(remote_contact, na.rm=T)), by='week_contact']
##     week_contact V1 V2 V3
##  1:   2020-06-29  4  0  0
##  2:   2020-07-06  6  2  0
##  3:   2020-07-13  4  3  0
##  4:   2020-07-20  1  7  0
##  5:   2020-07-27  3  0  3
##  6:   2020-08-03  2  1  5
##  7:   2020-08-10  4  1  5
##  8:   2020-08-17  2  2  2
##  9:   2020-08-24  2  4  2
## 10:   2020-08-31  0  1  1
## 11:   2020-09-07  1  0  1
## 12:   2020-09-14  5  5  3
## 13:   2020-09-21  7  3  1
## 14:   2020-09-28  0  8  5
## 15:   2020-11-02  0  9  6
## 16:   2020-10-05  5  4  5
## 17:   2020-10-12  4  2  6
## 18:   2020-10-19  5  4  6
## 19:   2020-10-26  3  3  5
## 20:   2020-11-09  7  2 10
## 21:   2020-11-16  6  3  8
## 22:   2020-11-23  1  5  7
## 23:   2020-11-30  3  7 10
## 24:   2020-12-07  1  5  7
## 25:   2020-12-14  6  4 15
## 26:   2020-12-21  1  2 10
## 27:   2020-12-28  4  4  5
## 28:   2021-01-04  6  4 16
## 29:   2021-01-18  1  5 13
## 30:   2021-01-11  2  8 19
## 31:   2021-02-01  2  4 10
## 32:   2020-06-22  0  1  0
## 33:   2020-06-15  1  0  0
## 34:   2021-01-25  3  7 12
## 35:   2021-02-08  1  4  3
## 36:   2021-02-15  5  0  0
## 37:   2021-02-22  2  0  0
## 38:   2021-03-01  1  0  0
##     week_contact V1 V2 V3
redcap_pilot = fread("~/Downloads/CGM/CGMPilotStudyCGMInNe_DATA_2021-03-03_2025.csv")
redcap_pilot = redcap_pilot[!is.na(visit_date) | !is.na(date_contact)]
redcap_pilot = unique(redcap_pilot[,.(
  pop="Pilot",record_id, 
  week_contact = as.Date(as.character(cut(fcoalesce(as.Date(visit_date), as.Date(date_contact, "%m/%d/%y")), "week"))),
  in_person_visit = as.numeric(visit_type) == 1,
  telehealth_visit = as.numeric(visit_type) == 2,
  remote_contact = !is.na(date_contact) & contact_week==1
  )])
redcap_pilot = redcap_pilot[!is.na(week_contact)]
# str(redcap_pilot)
redcap_pilot[,.(sum(in_person_visit, na.rm=T), sum(telehealth_visit, na.rm=T), sum(remote_contact, na.rm=T)), by='week_contact']
##      week_contact V1 V2 V3
##   1:   2018-10-08  0  0  1
##   2:   2018-11-12  1  0  0
##   3:   2019-01-28  0  0  0
##   4:   2019-04-29  1  0  6
##   5:   2019-09-30  7  2 26
##  ---                      
## 136:   2018-06-11  0  0  0
## 137:   2021-01-04  0  0 11
## 138:   2021-02-08  0  0  4
## 139:   2002-11-11  0  0  1
## 140:   2029-02-12  0  0  0
redcap_tips = fread("~/Downloads/CGM/CGMTips_DATA_2021-03-06_1131.csv")
redcap_tips = redcap_tips[!is.na(visit_date) | !is.na(date_contact)]
redcap_tips = unique(redcap_tips[,.(
  pop="TIPS", record_id, 
  week_contact = as.Date(as.character(cut(fcoalesce(as.Date(visit_date), as.Date(date_contact, "%m-%d-%y")), "week"))),
  in_person_visit = as.numeric(visit_type) == 1,
  telehealth_visit = as.numeric(visit_type) == 2,
  remote_contact = !is.na(date_contact) & contact_week==1)])
#str(redcap_tips)
redcap_tips = redcap_tips[!is.na(week_contact)]
redcap_tips[,.(sum(in_person_visit, na.rm=T), sum(telehealth_visit, na.rm=T), sum(remote_contact, na.rm=T)), by='week_contact']
##     week_contact V1 V2 V3
##  1:   2020-10-26  2  0  2
##  2:   2020-09-14  0  0  1
##  3:   2020-10-12  3  2  3
##  4:   2020-12-07  1  3  5
##  5:   2020-01-13  0  0  8
##  6:   2020-08-31  0  1  1
##  7:   2020-11-23  1  4  2
##  8:   2021-01-18  1  3  0
##  9:   2020-10-19  2  0  1
## 10:   2020-11-16  3  4  1
## 11:   2020-12-14  1  7  4
## 12:   2020-12-21  1  2  3
## 13:   2020-12-28  2  0  0
## 14:   2021-01-04  1  3  0
## 15:   2020-09-28  0  1  0
## 16:   2020-11-30  2  2  2
## 17:   2020-09-07  2  0  1
## 18:   2020-10-05  1  0  1
## 19:   2020-11-02  3  3  2
## 20:   2020-09-21  1  0  0
## 21:   2020-11-09  1  2  2
## 22:   2021-01-11  1  2  0
## 23:   2020-01-06  0  0  3
## 24:   2020-01-20  0  0  3
##     week_contact V1 V2 V3
# Combine contacts
redcap_combo = rbind(redcap_pilot, redcap_4t, redcap_tips)
# str(redcap_combo)

contacts_agg = redcap_combo[,.(
  in_person_visits = sum(in_person_visit, na.rm=T),
  telehealth_visits = sum(telehealth_visit, na.rm=T),
  remote_contacts = sum(remote_contact, na.rm=T)
  ), by=c('pop','record_id')]

contacts_agg
##        pop record_id in_person_visits telehealth_visits remote_contacts
##   1: Pilot         1                2                 5               2
##   2: Pilot         2                3                 2               0
##   3: Pilot         3                2                 4               0
##   4: Pilot         4                3                 2               0
##   5: Pilot         5                3                 2               0
##  ---                                                                   
## 244:  TIPS        40                0                 0               1
## 245:  TIPS        41                0                 2               1
## 246:  TIPS        43                0                 1               1
## 247:  TIPS        45                0                 1               1
## 248:  TIPS        50                0                 0               1
t1dc = merge(t1d, contacts_agg, by=c('pop','record_id'), all.x=T)
t1dc = merge(t1dc, redcap_pump, by=c('pop','record_id'), all.x=T)
t1dc[is.na(in_person_visits), in_person_visits:=0]
t1dc[is.na(telehealth_visits), telehealth_visits:=0]
t1dc[is.na(remote_contacts), remote_contacts:=0]
t1dc[is.na(pump), pump:=0]

Try using Table1

library(table1)
## 
## Attaching package: 'table1'
## The following objects are masked from 'package:base':
## 
##     units, units<-
table1(
  ~ sex + insurance_type + age + num_bg_recodings + num_dates + num_weeks + in_person_visits + telehealth_visits + remote_contacts + pump | pop,
  render.continuous=c(.="Mean (SD)", "Median [IQR]"="Median [Q1, Q3]"),
  data=t1dc,
  overall = "Total",
  droplevels = TRUE)
4T
(N=58)
Pilot
(N=129)
TIPS
(N=63)
Total
(N=250)
sex
Female 28 (48.3%) 60 (46.5%) 34 (54.0%) 122 (48.8%)
Male 30 (51.7%) 69 (53.5%) 29 (46.0%) 128 (51.2%)
insurance_type
Both 1 (1.7%) 0 (0%) 3 (4.8%) 4 (1.6%)
Private 39 (67.2%) 100 (77.5%) 5 (7.9%) 144 (57.6%)
Public 18 (31.0%) 28 (21.7%) 55 (87.3%) 101 (40.4%)
Missing 0 (0%) 1 (0.8%) 0 (0%) 1 (0.4%)
age
Mean (SD) 10.4 (4.60) 10.0 (4.57) 13.0 (4.46) 10.9 (4.70)
Median [IQR] 11.0 [8.00, 14.0] 10.0 [7.00, 14.0] 13.0 [11.0, 16.0] 11.0 [8.00, 14.0]
num_bg_recodings
Mean (SD) 39800 (53500) 125000 (62700) 45700 (73500) 85000 (75500)
Median [IQR] 33500 [19200, 46100] 116000 [81900, 160000] 20100 [8820, 39900] 66300 [23300, 129000]
num_dates
Mean (SD) 148 (196) 471 (220) 195 (284) 327 (277)
Median [IQR] 126 [69.3, 166] 430 [323, 628] 90.0 [47.0, 194] 258 [98.0, 502]
num_weeks
Mean (SD) 22.9 (28.2) 71.7 (30.7) 32.8 (42.3) 50.6 (40.0)
Median [IQR] 20.0 [11.0, 27.0] 66.0 [53.0, 96.0] 17.0 [10.0, 34.0] 41.0 [17.3, 75.8]
in_person_visits
Mean (SD) 1.59 (0.956) 2.84 (1.80) 0.460 (1.12) 1.95 (1.79)
Median [IQR] 1.00 [1.00, 2.00] 3.00 [2.00, 4.00] 0 [0, 0] 2.00 [1.00, 3.00]
telehealth_visits
Mean (SD) 2.12 (1.57) 2.38 (1.61) 0.508 (0.965) 1.85 (1.66)
Median [IQR] 2.00 [1.00, 3.00] 2.00 [1.00, 3.00] 0 [0, 1.00] 2.00 [0, 3.00]
remote_contacts
Mean (SD) 3.47 (4.13) 14.6 (14.1) 0.651 (1.08) 8.52 (12.1)
Median [IQR] 2.00 [0, 5.75] 13.0 [0, 26.0] 0 [0, 1.00] 2.00 [0, 14.0]
pump
Mean (SD) 0.172 (0.381) 0.504 (0.502) 0.0794 (0.272) 0.320 (0.467)
Median [IQR] 0 [0, 0] 1.00 [0, 1.00] 0 [0, 0] 0 [0, 1.00]