flowchart LR sf[Storage Formats] sf1[Tree] sf2[Short and Wide] sf3[Tall and Thin] sf3a[Uniform Number<br>of Rows,<br>With NAs] sf3b[Variable Number<br>of Rows,<br>With Few NAs] sf --> sf1 & sf2 & sf3 sf3 --> sf3a & sf3b locf[Last<br>Observation<br>Carried<br>Forward] crr[Carry<br>Forward<br>by Adding<br>Rows] fi[Find First<br>Observation<br>Meeting Criteria] cf[Using Functions<br>That Count<br>Criteria Met] im[Inexact<br>Matching] sf3a --> locf sf3b --> crr & fi & cf & im
13 Manipulation of Longitudinal Data
The data.table
package provides exceptional capabilities for manipulating longitudinal data, especially when performing operations by subject. Before showing a variety of examples that typify common tasks, consider that there are many ways to store longitudinal data, including
- as an R
list
hierarchical tree with one branch per subject (not considered here) - “short and wide” with one column per time point (not considered here because this setup requires much more metadata setup, more storage space, and more coding)
- “tall and thin” with one row per subject per time point observed (the primary format considered in this chapter; typically there are few
NA
s unless many response variables are collected at a single time and some of them areNA
) - “tall and thin” with one row per subject per time point potentially observed, with missing values (
NA
) for unobserved measurements (considered in the first example)
13.1 Uniform Number of Rows
Consider first the case where most of the subjects have the same number of rows and NA
is used as a placeholder with a certain measurement is not made on a given time. Though highly questionable statistically, last observation carried forward (LOCF) is sometimes used to fill in NA
s so that simple analyses can be performed.
data.table
has an efficient built-in functions for LOCF (and for last observation carried backward and fill-in using a constant value): nafill
and setnafill
. Consider a longitudinal data table L
with 5 observations per each of two subjects.
require(Hmisc)
require(data.table)
<- data.table(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2),
L day = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
y1 = 1:10,
y2 = c(NA, 1, NA, NA, 2, 1, 2, 3, 4, NA),
key = .q(id, day))
# .q(id, day) is the Hmisc version of c('id', 'day')
L
id day y1 y2
1: 1 1 1 NA
2: 1 2 2 1
3: 1 3 3 NA
4: 1 4 4 NA
5: 1 5 5 2
6: 2 1 6 1
7: 2 2 7 2
8: 2 3 8 3
9: 2 4 9 4
10: 2 5 10 NA
setnafill(L, "locf", cols=.q(y1, y2))
L
id day y1 y2
1: 1 1 1 NA
2: 1 2 2 1
3: 1 3 3 1
4: 1 4 4 1
5: 1 5 5 2
6: 2 1 6 1
7: 2 2 7 2
8: 2 3 8 3
9: 2 4 9 4
10: 2 5 10 4
setnafill
changed the data table in place. Note that y1
is unchanged since it contained no NA
s.
13.2 Variable Number of Rows
Consider the somewhat more frequently occuring situation where there is one row per subject per time at which a measurement is made. Consider a different data table L
, and create records to fill out the observations, carrying forward to 4 days the subject’s last observation on y
if it was assessed earlier than day 4.
<- data.table(id = c(2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 5),
L day = c(1, 2, 3, 1, 2, 3, 4, 1, 2, 1, 2, 3),
y = 1 : 12, key='id')
<- copy(L) # fresh start with no propagation of changes back to L
w # Only needed if will be using := to compute variables in-place and
# you don't want the new variables also added to L
# This is related to data.table doing things by reference instead of
# making copies. w <- L does not create new memory for w.
# Compute each day's within-subject record number and last record number
# Feed this directly into a data.table operation to save last records
# when the last is on a day < 4
<- w[, .q(seq, maxseq) := .(1 : .N, .N), by=id][seq == maxseq & day < 4,]
u # Extra observations to fill out to day 4
<- u[, .(day = (day + 1) : 4, y = y), by=id]
u u
id day y
1: 2 4 3
2: 4 3 9
3: 4 4 9
4: 5 4 12
<- rbind(L, u, fill=TRUE)
w setkey(w, id, day) # sort and index
w
id day y
1: 2 1 1
2: 2 2 2
3: 2 3 3
4: 2 4 3
5: 3 1 4
6: 3 2 5
7: 3 3 6
8: 3 4 7
9: 4 1 8
10: 4 2 9
11: 4 3 9
12: 4 4 9
13: 5 1 10
14: 5 2 11
15: 5 3 12
16: 5 4 12
Find the first time at which y >= 3 and at which y >= 7.
day[y >= 3]
is read as “the value of day
when y >= 3
”. It is a standard subscripting operation in R for two parallel vectors day
and y
. Taking the minimum value of day
satisfying the condition gives us the first qualifying day.first3 = min(day[y >= 3]),
L[, .(first7 = min(day[y >= 7])), by=id]
id first3 first7
1: 2 3 Inf
2: 3 1 4
3: 4 1 1
4: 5 1 1
Same but instead of resulting in an infinite value if no observations for a subject meet the condition, make the result NA
.
<- function(x) if(length(x)) min(x) else as.double(NA)
mn # as.double needed because day is stored as double precision
# (type contents(L) to see this) and data.table requires
# consistent storage types
first3 = mn(day[y >= 3]),
L[, .(first7 = mn(day[y >= 7])), by=id]
id first3 first7
1: 2 3 NA
2: 3 1 4
3: 4 1 1
4: 5 1 1
Add a new variable z
and compute the first day at which z
is above 0.5 for two days in a row for the subject. Note that the logic below looks for consecutive days for which records exist for a subject. To also require the days to be one day apart add the clause day == shift(day) + 1
after shift(z) > 0.5
.
set.seed(1)
<- copy(L)
w := round(runif(.N), 3)]
w[, z <- copy(w)
u u
id day y z
1: 2 1 1 0.266
2: 2 2 2 0.372
3: 2 3 3 0.573
4: 3 1 4 0.908
5: 3 2 5 0.202
6: 3 3 6 0.898
7: 3 4 7 0.945
8: 4 1 8 0.661
9: 4 2 9 0.629
10: 5 1 10 0.062
11: 5 2 11 0.206
12: 5 3 12 0.177
<- function(x)
mn if(! length(x) || all(is.na(x))) as.double(NA) else min(x, na.rm=TRUE)
:= z > 0.5 & shift(z) > 0.5, by=id][,
u[, consecutive := mn(day[consecutive]), by=id]
firstday u
id day y z consecutive firstday
1: 2 1 1 0.266 FALSE NA
2: 2 2 2 0.372 FALSE NA
3: 2 3 3 0.573 FALSE NA
4: 3 1 4 0.908 NA 4
5: 3 2 5 0.202 FALSE 4
6: 3 3 6 0.898 FALSE 4
7: 3 4 7 0.945 TRUE 4
8: 4 1 8 0.661 NA 2
9: 4 2 9 0.629 TRUE 2
10: 5 1 10 0.062 FALSE NA
11: 5 2 11 0.206 FALSE NA
12: 5 3 12 0.177 FALSE NA
In general, using methods that involve counters makes logic more clear, easier to incrementally debug, and easier to extend the condition to any number of consecutive times. Create a function that computes the number of consecutive TRUE
values or ones such that whenever the sequence is interrupted by FALSE
or 0 the counting starts over. As before we compute the first day
at which two consecutive z
values exceed 0.5.
nconsec
is modified from code found here.<- function(x) x * sequence(rle(x)$lengths)
nconsec # rle in base R: run length encoding
# Example:
<- c(0,0,1,1,0,1,1,0,1,1,1,1)
x nconsec(x)
[1] 0 0 1 2 0 1 2 0 1 2 3 4
# To require that the time gap between measurements must be <= 2 time
# units use the following example
<- c(1:9, 11, 14, 15)
t rbind(t=t, x=x)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
t 1 2 3 4 5 6 7 8 9 11 14 15
x 0 0 1 1 0 1 1 0 1 1 1 1
nconsec(x & t <= shift(t) + 2)
[1] 0 0 1 2 0 1 2 0 1 2 0 1
<- copy(w)
u # nconsec(z > 0.5) = number of consecutive days (counting current
# day) for which the subject had z > 0.5
:= mn(day[nconsec(z > 0.5) == 2]), by=id]
u[, firstday # | | | |
# minimum | | |
# day | |
# such that |
# it's the 2nd consecutive day with z > 0.5
u
id day y z firstday
1: 2 1 1 0.266 NA
2: 2 2 2 0.372 NA
3: 2 3 3 0.573 NA
4: 3 1 4 0.908 4
5: 3 2 5 0.202 4
6: 3 3 6 0.898 4
7: 3 4 7 0.945 4
8: 4 1 8 0.661 2
9: 4 2 9 0.629 2
10: 5 1 10 0.062 NA
11: 5 2 11 0.206 NA
12: 5 3 12 0.177 NA
13.3 Overlap Joins and Non-equi Merges
Section 12.2 covered simple inexact matching/merging. Now consider more complex tasks.
The foverlaps
function in data.table
provides an amazingly fast way to do complex overlap joins. Our first example is modified from an example in the help file for foverlaps
. An annotation column is added to describe what happened.
<- data.table(w =.q(a, a, b, b, b),
d1 start = c( 5, 10, 1, 25, 50),
end = c(11, 20, 4, 52, 60))
<- data.table(w =.q(a, a, b),
d2 start = c(1, 15, 1),
end = c(4, 18, 55),
name = .q(dog, cat, giraffe),
key = .q(w, start, end))
<- foverlaps(d1, d2, type="any")
f <- c('no a overlap with d1 5-11 & d2 interval',
ann 'a 10-20 overlaps with a 16-18',
'b 1-4 overlaps with b 1-55',
'b 25-62 overlaps with b 1-55',
'b 50-60 overlaps with b 1-55')
:= ann]
f[, annotation f
w start end name i.start i.end annotation
1: a NA NA <NA> 5 11 no a overlap with d1 5-11 & d2 interval
2: a 15 18 cat 10 20 a 10-20 overlaps with a 16-18
3: b 1 55 giraffe 1 4 b 1-4 overlaps with b 1-55
4: b 1 55 giraffe 25 52 b 25-62 overlaps with b 1-55
5: b 1 55 giraffe 50 60 b 50-60 overlaps with b 1-55
# Don't include record for non-match
foverlaps(d1, d2, type='any', nomatch=NULL)
w start end name i.start i.end
1: a 15 18 cat 10 20
2: b 1 55 giraffe 1 4
3: b 1 55 giraffe 25 52
4: b 1 55 giraffe 50 60
# Require the d1 interval to be within the d2 interval
foverlaps(d1, d2, type="within")
w start end name i.start i.end
1: a NA NA <NA> 5 11
2: a NA NA <NA> 10 20
3: b 1 55 giraffe 1 4
4: b 1 55 giraffe 25 52
5: b NA NA <NA> 50 60
# Require the intervals to have the same starting point
foverlaps(d1, d2, type="start")
w start end name i.start i.end
1: a NA NA <NA> 5 11
2: a NA NA <NA> 10 20
3: b 1 55 giraffe 1 4
4: b NA NA <NA> 25 52
5: b NA NA <NA> 50 60
Now consider an example where there is an “events” dataset e
with 0 or more rows per subject containing start (s
) and end (e
) times and a measurement x
representing a daily dose of something given to the subject from s
to e
. The base dataset b
has one record per subject with times c
and d
. Compute the total dose of drug received between c
and d
for the subject. This is done by finding all records in e
for the subject such that the interval [c,d]
has any overlap with the interval [s,e]
. For each match compute the number of days in the interval [s,e]
that are also in [c,d]
. This is given by min(e,d) + 1 - max(c,s)
. Multiply this duration by x
to get the total dose given in [c,d]
. For multiple records with intervals touching [c,d]
add these products.
<- data.table(id = .q(a,b,c), low=10, hi=20)
base <- data.table(id = .q(a,b,b,b,k),
events start = c( 8, 7, 12, 19, 99),
end = c( 9, 8, 14, 88, 99),
dose = c(13, 17, 19, 23, 29))
setkey(base, id, low, hi)
setkey(events, id, start, end)
<- foverlaps(base, events,
w by.x = .q(id, low, hi),
by.y = .q(id, start, end ),
type = 'any', mult='all', nomatch=NA)
:= pmin(end, hi) + 1 - pmax(start, low)]
w[, elapsed total.dose = sum(dose * elapsed, na.rm=TRUE)), by=id] w[, .(
id total.dose
1: a 0
2: b 103
3: c 0
Similar things are can be done with non-equi merges. For those you can require exact subject matches but allow inexact matches on other variables. The following example is modified from here. A medication
dataset holds the start and end dates for a patient being on a treatment, and a second dataset visit
has one record per subject ID per doctor visit. For each visit look up the drug in effect if there was one.
<-
medication data.table(ID = c( 1, 1, 2, 3, 3),
medication = .q(a, b, a, a, b),
start = as.Date(c("2003-03-25","2006-04-27","2008-12-05",
"2004-01-03","2005-09-18")),
stop = as.Date(c("2006-04-02","2012-02-03","2011-05-03",
"2005-06-30","2010-07-12")),
key = 'ID')
medication
ID medication start stop
1: 1 a 2003-03-25 2006-04-02
2: 1 b 2006-04-27 2012-02-03
3: 2 a 2008-12-05 2011-05-03
4: 3 a 2004-01-03 2005-06-30
5: 3 b 2005-09-18 2010-07-12
set.seed(123)
<- data.table(
visit ID = rep(1:3, 4),
date = sample(seq(as.Date('2003-01-01'), as.Date('2013-01-01'), 1), 12),
sbp = round(rnorm(12, 120, 15)),
key = c('ID', 'date'))
visit
ID date sbp
1: 1 2004-06-09 113
2: 1 2006-06-06 147
3: 1 2008-01-16 126
4: 1 2009-09-28 127
5: 2 2003-07-14 138
6: 2 2006-02-15 122
7: 2 2006-06-21 127
8: 2 2009-11-15 101
9: 3 2005-11-03 91
10: 3 2009-02-04 110
11: 3 2011-03-05 125
12: 3 2012-03-24 112
# Variables named in inequalities need to have variables in
# medication listed first
<- medication[visit, on = .(ID, start <= date, stop > date)]
m m
ID medication start stop sbp
1: 1 a 2004-06-09 2004-06-09 113
2: 1 b 2006-06-06 2006-06-06 147
3: 1 b 2008-01-16 2008-01-16 126
4: 1 b 2009-09-28 2009-09-28 127
5: 2 <NA> 2003-07-14 2003-07-14 138
6: 2 <NA> 2006-02-15 2006-02-15 122
7: 2 <NA> 2006-06-21 2006-06-21 127
8: 2 a 2009-11-15 2009-11-15 101
9: 3 b 2005-11-03 2005-11-03 91
10: 3 b 2009-02-04 2009-02-04 110
11: 3 <NA> 2011-03-05 2011-03-05 125
12: 3 <NA> 2012-03-24 2012-03-24 112
# start and stop dates are replaced with actual date of visit
# drop one of them and rename the other
:= NULL]
m[, stop setnames(m, 'start', 'date')
m
ID medication date sbp
1: 1 a 2004-06-09 113
2: 1 b 2006-06-06 147
3: 1 b 2008-01-16 126
4: 1 b 2009-09-28 127
5: 2 <NA> 2003-07-14 138
6: 2 <NA> 2006-02-15 122
7: 2 <NA> 2006-06-21 127
8: 2 a 2009-11-15 101
9: 3 b 2005-11-03 91
10: 3 b 2009-02-04 110
11: 3 <NA> 2011-03-05 125
12: 3 <NA> 2012-03-24 112