In R
, I have three time points
time <- c(7, 1, 4)
and suppose that the time period is partitioned into three intervals: (0, 3], (3, 5], (5, 8]
breaks <- c(3, 5, 8)
timeSpent
has one row for each observation and one column for each period. It gives the time spent by each observation in each period:
timeSpent <- outer(X=time, Y=breaks, FUN=pmin)
timeSpent <- cbind(timeSpent[, 1],
sapply(X=1:(length(breaks) - 1), FUN=function(ii)
timeSpent[, ii + 1] - timeSpent[, ii]))
> timeSpent
[,1] [,2] [,3]
[1,] 3 2 2
[2,] 1 0 0
[3,] 3 1 0
For example, observation 1 spent 3 days in interval 1, 2 days in interval 2, and 2 more days in interval 3. For observation 2, it spent only 1 day in interval 1, and nothing in the remaining intervals.
Would you have a more elegant way to obtain timeSpent
?