I have the following example data:
library(data.table)
set.seed(42)
t <- data.table(time=1:1000, period=round(runif(100,1,5)))
p <- data.table(id=1:10, cut=sample(1:100,5))
> t[62:71]
time period
1: 62 5
2: 63 4
3: 64 3
4: 65 4
5: 66 2
6: 67 2
7: 68 4
8: 69 4
9: 70 2
10: 71 1
> head(p)
id cut
1: 1 63
2: 2 22
3: 3 99
4: 4 38
5: 5 91
6: 6 63
where t
gives some vector of periods
associated with time points, and p
gives for each person a cutoff in time
.
For each person in p
, I would like to start at the person's cutoff and create a sequence of 4 time points by concatenating the periods
. For example, for person 1, starting at time 63, the sequence would be 63
, 63+4=67
, 67+2=69
and 69+4=73
.
Ideally, the output would then be:
> head(res)
id t1 t2 t3 t4
1 63 67 69 73
2 22 24 29 32
3 99 103 105 109
4 38 40 43 44
5 91 95 100 103
6 63 67 69 73
I learned before how to create the sequences using accumulate::purrr
(iterative cumsum where sum determines the next position to be added). However, I wonder whether something like this can be done simultaneously for different persons using data.table
or other packages but avoiding for-loops as the datasets are rather large.
edit: version where time values do not coincide with row indicies
library(data.table)
set.seed(42)
t <- data.table(time=1001:2000, period=round(runif(100,1,5)))
p <- data.table(id=1:10, cut=sample(1:100,5))
is similar as above, except for
> t[62:71]
time period
1: 1062 5
2: 1063 4
3: 1064 3
4: 1065 4
5: 1066 2
6: 1067 2
7: 1068 4
8: 1069 4
9: 1070 2
10: 1071 1
where t$time[i]
does not equal i
, which prohibits Jaap's first solution.