[英]Reducing time series data from half hour to hourly in R
我正在處理半小時分辨率的智能電表數據。 由於數據量龐大,我試圖將每半小時的分辨率降低到每小時的分辨率。 這樣做時,我試圖對兩次每半小時測量的消耗量求和。 問題是我的數據框中也有分類數據,在使用 xts 時會丟失這些數據。 這是我的數據的樣子:
> head(test1)
LCLid stdorToU DateTime KWH.hh..per.half.hour. Acorn Acorn_grouped
1 MAC000002 Std 2012-10-12 00:30:00 0 ACORN-A Affluent
2 MAC000002 Std 2012-10-12 01:00:00 0 ACORN-A Affluent
3 MAC000002 Std 2012-10-12 01:30:00 0 ACORN-A Affluent
4 MAC000002 Std 2012-10-12 02:00:00 0 ACORN-A Affluent
5 MAC000002 Std 2012-10-12 02:30:00 0 ACORN-A Affluent
6 MAC000002 Std 2012-10-12 03:00:00 0 ACORN-A Affluent
這是我一直在嘗試使用的代碼和我得到的結果。
test1 <- read.csv("test.csv", stringsAsFactors = F)
test1$DateTime <- ymd_hms(test1$DateTime)
test1$KWH.hh..per.half.hour. <- as.numeric(test1$KWH.hh..per.half.hour.)
test2 <- xts(test1$KWH.hh..per.half.hour., test1$DateTime)
head(test2)
period.apply(test2, endpoints(test2, "hours"), sum)
> period.apply(test2, endpoints(test2, "hours"), sum)
[,1]
2012-10-12 00:30:00 0.000
2012-10-12 01:30:00 0.000
2012-10-12 02:30:00 0.000
2012-10-12 03:30:00 0.000
2012-10-12 04:30:00 0.000
2012-10-12 05:30:00 0.000
2012-10-12 06:30:00 0.000
2012-10-12 07:30:00 0.000
2012-10-12 08:30:00 0.000
2012-10-12 09:30:00 0.000
2012-10-12 10:30:00 0.000
理想情況下,我需要一個與我的原始數據集 (test1) 完全相同的數據集,只是聚合到每小時頻率而不是半小時頻率的一半。 有人可以幫忙嗎。
謝謝
您需要創建一個分組列,然后按組求和。
# create grouped column
test1$grouped_time = lubridate::floor_date(test1$DateTime, unit = "hour")
# (use ceiling_date instead if you want to round the half hours up instead of down)
# sum by group
library(dplyr)
test2 = test1 %>%
group_by(grouped_time, LCLid, stdorToU, Acorn, Acorn_grouped) %>%
summarize(KWH.hh.per.hour = sum(KWH.hh..per.half.hour.))
如果您想查看更多選項,則在Sum by Group R-FAQ 中有許多替代dplyr
的選項。
請注意,這將對group_by()
其他列的每個唯一組合的 KWH 列求和。 如果一些人可以改變,就像如果stdorToU
或ACORN
值可能從一個小時改變到下一個半小時,但你還是要行結合起來,你需要移動柱而出的group_by
進入summarize
,並指定其價值保持,例如
# if ACORN can change and you want to keep the first one
test2 = test1 %>%
group_by(grouped_time, LCLid, stdorToU, Acorn_grouped) %>%
summarize(KWH.hh.per.hour = sum(KWH.hh..per.half.hour.),
ACORN = first(ACORN))
> head(sm_2013_tof)
# A tibble: 6 x 6
# Groups: grouped_time, LCLid, stdorToU, Acorn [6]
grouped_time LCLid stdorToU Acorn Acorn_grouped KWH.hh.per.hour
<dttm> <chr> <chr> <chr> <chr> <dbl>
1 2013-01-01 00:00:00 MAC000146 ToU ACORN-L Adversity 0.155
2 2013-01-01 00:00:00 MAC000147 ToU ACORN-F Comfortable 0.276
3 2013-01-01 00:00:00 MAC000158 ToU ACORN-H Comfortable 0.152
4 2013-01-01 00:00:00 MAC000165 ToU ACORN-E Affluent 0.401
5 2013-01-01 00:00:00 MAC000170 ToU ACORN-F Comfortable 0.64
6 2013-01-01 00:00:00 MAC000173 ToU ACORN-E Affluent 0.072
>
這是分組后的每小時數據。
如果我將其設為 as.data.frame,您會看到 00:00:00 消失了
sm_short_2013 <- as.data.frame(sm_2013_tof)
> head(sm_short_2013)
grouped_time LCLid stdorToU Acorn Acorn_grouped KWH.hh.per.hour
1 2013-01-01 MAC000146 ToU ACORN-L Adversity 0.155
2 2013-01-01 MAC000147 ToU ACORN-F Comfortable 0.276
3 2013-01-01 MAC000158 ToU ACORN-H Comfortable 0.152
4 2013-01-01 MAC000165 ToU ACORN-E Affluent 0.401
5 2013-01-01 MAC000170 ToU ACORN-F Comfortable 0.640
6 2013-01-01 MAC000173 ToU ACORN-E Affluent 0.072
> dput(droplevels(sm_short_2013[1:10, ]))
structure(list(grouped_time = structure(c(1356998400, 1356998400,
1356998400, 1356998400, 1356998400, 1356998400, 1356998400, 1356998400,
1356998400, 1356998400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
LCLid = c("MAC000146", "MAC000147", "MAC000158", "MAC000165",
"MAC000170", "MAC000173", "MAC000186", "MAC000187", "MAC000193",
"MAC000194"), stdorToU = c("ToU", "ToU", "ToU", "ToU", "ToU",
"ToU", "ToU", "ToU", "ToU", "ToU"), Acorn = c("ACORN-L",
"ACORN-F", "ACORN-H", "ACORN-E", "ACORN-F", "ACORN-E", "ACORN-E",
"ACORN-L", "ACORN-D", "ACORN-D"), Acorn_grouped = c("Adversity",
"Comfortable", "Comfortable", "Affluent", "Comfortable",
"Affluent", "Affluent", "Adversity", "Affluent", "Affluent"
), KWH.hh.per.hour = c(0.155, 0.276, 0.152, 0.401, 0.64,
0.072, 0.407, 0.554, 0.725, 0.158)), row.names = c(NA, 10L
), class = "data.frame")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.