【发布时间】:2013-08-06 08:28:41
【问题描述】:
在R,我有三个时间点
时间
并假设时间段被划分为三个区间:(0, 3], (3, 5], (5, 8]
breaks <- c(3, 5, 8)
timeSpent 为每个观察值一行,每个周期为一列。它给出了每个时期每个观察所花费的时间:
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
例如,观察 1 在间隔 1 中花费了 3 天,在间隔 2 中花费了 2 天,在间隔 3 中又花费了 2 天。对于观察 2,它在间隔 1 中仅花费了 1 天,其余间隔中没有任何内容。
你有更优雅的方式来获取timeSpent吗?
【问题讨论】:
-
你如何决定一个人在一个时间段内花费多少时间?
-
拿个人1。他在7天后离开。因此,他完全通过了第 1 期和第 2 期,部分通过了第 3 期。在第 1 期,他花费了:3 - 0。在第 2 期,他花费了:5 - 3。在第 3 期,他花费了:7 - 5。
标签: r data-manipulation