Question

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?

Was it helpful?

Solution

Does this help?

We cbind a column of zeros to the timeSpent matrix so we can get the initial time spent in the first observation window, and apply the diff function across the rows...

res <- t( apply( cbind( 0 , timeSpent ) , 1 , diff ) )
     [,1] [,2] [,3]
[1,]    3    2    2
[2,]    1    0    0
[3,]    3    1    0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top