问题描述
我有一个时间序列的连续数据,每 10 分钟测量一次,持续五个月.为简单起见,数据分为两列,如下所示:
时间戳 Temp.Diff2/14/2011 19:00 -0.3852/14/2011 19:10 -0.5352/14/2011 19:20 -0.4842/14/2011 19:30 -0.4092/14/2011 19:40 -0.3852/14/2011 19:50 -0.215... 在接下来的五个月里,这种情况会持续下去.我已经使用 as.POSIXct
解析了 Timestamp 列.
我想选择一天中特定时间的行(例如从中午 12 点到下午 3 点),我想排除一天中的其他时间,或者只是提取这 3 个小时但仍然有数据依次流动(即按时间序列).
您似乎了解基本思想,但只是缺少细节.正如您所提到的,我们只是将时间戳转换为 POSIX 对象,然后再进行子集化.
润滑液
最简单的方法可能是使用 lubridate.首先加载包:
library(lubridate)
接下来转换时间戳:
##*m*onth *d*ay *y*ear _ *h*our *m*inuted = mdy_hm(dd$Timestamp)
然后我们选择我们想要的.在这种情况下,我想要晚上 7:30 之后的任何日期(无论是哪一天):
dd[hour(d) == 19 &分钟(d)>30 |小时(d) >= 20,]
基础 R 解决方案
首先创建一个上限:
lower = strptime("2/14/2011 19:30","%m/%d/%Y %H:%M")
接下来转换 POSIX 对象中的时间戳:
d = strptime(dd$Timestamp, "%m/%d/%Y %H:%M")
最后,有点数据帧子集:
dd[format(d,"%H:%M") >格式(较低,%H:%M"),]
感谢 plannapus 的最后一部分
以上示例的数据:
dd = read.table(textConnection('Timestamp Temp.Diff2/14/2011 19:00"-0.3852/14/2011 19:10"-0.5352/14/2011 19:20"-0.4842/14/2011 19:30"-0.4092/14/2011 19:40"-0.385"2/14/2011 19:50" -0.215'), header=TRUE)
I have a time series of continuous data measured at 10 minute intervals for a period of five months. For simplicity's sake, the data is available in two columns as follows:
Timestamp Temp.Diff 2/14/2011 19:00 -0.385 2/14/2011 19:10 -0.535 2/14/2011 19:20 -0.484 2/14/2011 19:30 -0.409 2/14/2011 19:40 -0.385 2/14/2011 19:50 -0.215
... And it goes on for the next five months. I have parsed the Timestamp column using as.POSIXct
.
I want to select rows with certain times of the day, (e.g. from 12 noon to 3 PM), I would like either like to exclude the other hours of the day, OR just extract those 3 hours but still have the data flow sequentially (i.e. in a time series).
You seem to know the basic idea, but are just missing the details. As you mentioned, we just transform the Timestamps into POSIX objects then subset.
lubridate Solution
The easiest way is probably with lubridate. First load the package:
library(lubridate)
Next convert the timestamp:
##*m*onth *d*ay *y*ear _ *h*our *m*inute
d = mdy_hm(dd$Timestamp)
Then we select what we want. In this case, I want any dates after 7:30pm (regardless of day):
dd[hour(d) == 19 & minute(d) > 30 | hour(d) >= 20,]
Base R solution
First create an upper limit:
lower = strptime("2/14/2011 19:30","%m/%d/%Y %H:%M")
Next transform the Timestamps in POSIX objects:
d = strptime(dd$Timestamp, "%m/%d/%Y %H:%M")
Finally, a bit of dataframe subsetting:
dd[format(d,"%H:%M") > format(lower,"%H:%M"),]
Thanks to plannapus for this last part
Data for the above example:
dd = read.table(textConnection('Timestamp Temp.Diff
"2/14/2011 19:00" -0.385
"2/14/2011 19:10" -0.535
"2/14/2011 19:20" -0.484
"2/14/2011 19:30" -0.409
"2/14/2011 19:40" -0.385
"2/14/2011 19:50" -0.215'), header=TRUE)
这篇关于排除一天中特定时间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!