您好,我試圖將數據削減到每月,每週,每日每時使用套以下時間戳數據
[1] "2015-12-31 13:54:00 AEDT" "2015-12-30 12:41:00 AEDT" "2015-12-30 07:14:00 AEDT" "2015-12-30 03:07:00 AEDT"
[5] "2015-12-30 02:27:00 AEDT" "2015-12-29 17:09:00 AEDT" "2015-12-29 17:07:00 AEDT" "2015-12-29 16:46:00 AEDT"
[9] "2015-12-29 16:17:00 AEDT" "2015-12-29 15:10:00 AEDT" "2015-12-29 14:22:00 AEDT" "2015-12-29 12:17:00 AEDT"
[13] "2015-12-28 12:45:00 AEDT" "2015-12-28 11:13:00 AEDT" "2015-12-27 21:25:00 AEDT" "2015-12-27 20:25:00 AEDT"
我用下面從要素轉換爲posixlt日期格式:
td$datetime <- as.POSIXlt(as.character(td$time), format="%Y-%m-%d %H:%M")
str(td)
datetime : POSIXlt, format: "2015-12-31 13:54:00" "2015-12-30 12:41:00" "2015-12-30 07:14:00" "2015-12-30 03:07:00" ...
以下3都做工精細:
# create a column for month time break
td$month <- as.Date(cut(td$datetime, breaks = "month"))
# create a column for weekly time break
td$week <- as.Date(cut(td$datetime, breaks = "week", start.on.monday = FALSE))
# create a column for daily time break
td$day <- as.Date(cut(td$datetime, breaks = "day"))
但是當我做每小時,這是行不通的
td$hour <- as.Date(cut(as.character(td$datetime), breaks = "hour")).
I get the following error
Error in cut.default(as.character(td$datetime), breaks = "Hour") :
'x' must be numeric
無需轉換爲字符和日期。嘗試:cut(td $ datetime,breaks =「hour」) – Dave2e
嗨戴夫,我試過了 - 看起來切入日,周,月的工作非常好,但是切入幾小時或者產生一天的切換或者一個錯誤,取決於對我的嘗試。最新我嘗試的是如下所示:td $ hour < - as.Date.character(cut(td $ datetime,breaks =「hour」))then t_freq_day_hour < - table(format(td $ hour2,「%d-%b - %Y%H:%M「)))。日期時間爲posix ie:$ datetime:POSIXlt,格式:「2015-12-31 13:54:00」「2015-12-30 12:41:00」「2015-12-30 07:14:00」 「 – maric