r - R中的分箱时间数据

标签 r time binning

我有鸟类出发和到达的时间数据(例如 17:23:54 到达)。我想将数据分成 2 小时的时间段(例如 0:00:00-1:59:59 等),所以总共 12 个段。数据最终会进入一个条形图,其中 x 轴上的时间箱和 y 轴上的计数。包包“binr”是我最好的选择吗?

谢谢

最佳答案

只需使用 ?cut,因为它具有 ?cut.POSIXt 日期/时间的方法。例如:

x <- as.POSIXct("2016-01-01 00:00:00", tz="UTC") + as.difftime(30*(0:47),units="mins")
cut(x, breaks="2 hours", labels=FALSE)
# or to show more clearly the results:
data.frame(x, cuts = cut(x, breaks="2 hours", labels=FALSE))

#                     x cuts
#1  2016-01-01 00:00:00    1
#2  2016-01-01 00:30:00    1
#3  2016-01-01 01:00:00    1
#4  2016-01-01 01:30:00    1
#5  2016-01-01 02:00:00    2
#6  2016-01-01 02:30:00    2
#7  2016-01-01 03:00:00    2
#8  2016-01-01 03:30:00    2
#9  2016-01-01 04:00:00    3
#10 2016-01-01 04:30:00    3
# ...

如果您的数据只是字符串,那么您需要先进行转换。如果您不指定特定日期,时间最终将分配到当前日期。

as.POSIXct("17:23:54", format="%H:%M:%S", tz="UTC")
#[1] "2016-07-13 17:23:54 UTC"

关于r - R中的分箱时间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339812/

相关文章:

performance - 为什么在数据帧上运行 "unique"比 R 中的矩阵更快?

r - R 中的错​​误消息 "attempt to set an attribute on NULL"

php - 填充数组中缺失的元素

time - 无法通过USB将计算机时间同步到Arduino

python - 用 pandas 对列进行分箱

Matlab 二维密度图

matlab - 为 NaN 值创建 Bin

r - 使用 ggplot2 创建具有 95% CI 的平均加类时间变化的折线图

r - 在 R 中使用 mboost 多项式逻辑回归进行预测

Javascript 日期时间休息两小时