将数据拆分或子集为 30 分钟的时间间隔 [英] split or subset data into 30 minute intervals
问题描述
我有一个如下形式的数据框:
I have a data frame of the following form:
Temp Depth Light x time date time.at.depth
104 18.59 -2.7 27 21:38 2012-06-20 4
109 18.59 -2.7 27 22:02 2012-06-20 5
110 18.75 -4.0 27 22:07 2012-06-20 5
113 18.91 -2.7 27 22:21 2012-06-20 4
114 18.91 -4.0 27 22:26 2012-06-20 5
115 18.91 -2.7 27 22:31 2012-06-20 5
117 18.91 -2.7 27 22:40 2012-06-20 4
118 18.75 -4.0 27 22:45 2012-06-20 5
119 18.75 -2.7 27 22:50 2012-06-20 5
121 18.59 -4.0 27 22:59 2012-06-20 4
122 18.75 -2.7 27 23:04 2012-06-20 5
123 18.75 -4.0 27 23:09 2012-06-20 5
126 18.59 -2.7 27 23:23 2012-06-20 5
127 18.59 -2.7 27 23:28 2012-06-20 5
128 18.59 -4.0 27 23:33 2012-06-20 5
133 18.75 -4.0 27 23:57 2012-06-20 5
136 18.59 -4.0 27 00:11 2012-06-20 5
138 18.59 -2.7 27 00:21 2012-06-20 5
140 18.91 -2.7 27 00:30 2012-06-20 5
我想将数据子集划分为 30 分钟的区间.是否有捷径可寻?我查看了 split()
函数,但似乎需要采用数字格式才能工作,我有时间使用 POSIXct
格式.任何想法将不胜感激.
I would like to subset the data into 30 minute bins. Is there an easy way to do this? I looked at the split()
function but it appears that things need to be in a numeric format for that to work, I have time in POSIXct
format. Any ideas would be greatly appreciated.
推荐答案
这里有一个单行代码(dat
是你的数据框):
Here's a one-liner for you (dat
being your dataframe):
split(dat, cut(strptime(paste(dat$date, dat$time), format="%F %R"),"30 mins"))
确实 cut.POSIXt
接受作为中断值
Indeed cut.POSIXt
accepts as break values
间隔规范,秒"、分钟"、小时"、天"、DSTday"、周"、月"、季度"或年"之一,可选地以整数开头和一个空格,或后跟s".
an interval specification, one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year", optionally preceded by an integer and a space, or followed by "s".
另外,我使用 %F %R
作为 %Y-%m-%d %H:%M
的快捷方式:见 ?strptime
查看所有可能的格式.
Additionnaly, I used %F %R
as a shortcut for %Y-%m-%d %H:%M
: see ?strptime
to see all possible formats.
这篇关于将数据拆分或子集为 30 分钟的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!