使用lubridate包在R中创建间隔对象 [英] creating inteval object in r using lubridate package

查看:157
本文介绍了使用lubridate包在R中创建间隔对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自uber的数据:

关于纽约市的接载. 我试图在原始数据中添加一列,以指示每一行的 它属于哪个时间间隔(由时间间隔开始处的单个时间点表示).

我想创建一个包含所有相关时间点(即每15分钟一次)的向量 在此向量上使用lubridate包中的int_diff函数可创建一个 间隔对象. 对原始数据中的所有时间点以及每个数据运行一个循环 观点;指示到哪个时间间隔(由单个时间间隔表示 属于时间间隔开始的时间点).

我试图寻找有关如何使用int_diff函数的解释,但我不理解向量的外观以及int_diff的语法如何工作 坦克寻求帮助:)

解决方案

这是您要记住的吗?

start <- mdy_hm('4/11/2014 0:00') # start of the period
end <- mdy_hm('5/12/2015 0:00') # end
time_seq <- seq(from = start, to = end, by = '15 mins') # sequence by 15 minutes

times <- mdy_hm(c('4/11/2014 0:12', '4/11/2014 1:24')) # times to find intervals for
dat <- data.frame(times)

dat$intervals <- cut(times, breaks = time_seq) # assign each time to an interval

intervals_cols <- model.matrix(~ - + intervals, dat) # turn this into a set of columns, one for each interval, with a 1 indicating that this observation falls into the column

hi i have data from uber :

about pick ups in NYC . im trying to add a column to the raw data, that indicates for each row, for which time interval (which is represented by a single timepoint at the beginning of thetime interval) it belongs.

i want to Create a vector containing all relevant timepoints (i.e. every 15 minutes Use int_diff function from lubridate package on this vector to create an interval object. Run a loop on all the time points in the raw data and for each data point; indicate to which interval (which is represented by a single timepoint at the beginning of the time interval) it belongs.

i tried looking for explanations how to use the int_diff function but i dont understand how my vector should look and how the syntax of int_diff works tanks for the help :)

解决方案

Is this what you have in mind?

start <- mdy_hm('4/11/2014 0:00') # start of the period
end <- mdy_hm('5/12/2015 0:00') # end
time_seq <- seq(from = start, to = end, by = '15 mins') # sequence by 15 minutes

times <- mdy_hm(c('4/11/2014 0:12', '4/11/2014 1:24')) # times to find intervals for
dat <- data.frame(times)

dat$intervals <- cut(times, breaks = time_seq) # assign each time to an interval

intervals_cols <- model.matrix(~ - + intervals, dat) # turn this into a set of columns, one for each interval, with a 1 indicating that this observation falls into the column

这篇关于使用lubridate包在R中创建间隔对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆