簡體   English   中英

R-每小時時間序列中特定小時的NA

[英]R - NAs for specific hours on hourly time series

編輯:我遇到了另一個問題,因此編輯了一個問題:一方面將每小時數據匯總到每日平均值,另一方面每天過濾1個數據點(在16:00),之后,我得到了相同的數字個數據點(每天1個)。 但是,由於我想隱藏數據幀,因此如果在16:00到達數據點之前運行代碼,我將沒有多少行。 因此,我正在考慮如果沒有可用的數據點,則添加一行(帶有日期和NA值)。 我添加了代碼,那么它應該很有意義。

is.installed <- function(mypkg){
  is.element(mypkg, installed.packages()[,1])
} 
if (!is.installed("ggplot2")){
  install.packages("ggplot2")
}
if (!is.installed("lubridate")){
  install.packages("lubridate")
}
if (!is.installed("openxlsx")){
  install.packages("openxlsx")
}
library(ggplot2)
library(lubridate)
library(openxlsx)


Storico_G <- read.xlsx(xlsxFile = "http://www.snamretegas.it/repository/file/Info-storiche-qta-gas-trasportato/dati_operativi/2017/DatiOperativi_2017-IT.xlsx",sheet = "Storico_G", startRow = 1, colNames = TRUE)

Storico_G1 <- read.xlsx(xlsxFile = "http://www.snamretegas.it/repository/file/Info-storiche-qta-gas-trasportato/dati_operativi/2017/DatiOperativi_2017-IT.xlsx",sheet = "Storico_G+1", startRow = 1, colNames = TRUE)

# Selecting Column C,E,R from Storico_G and stored in variable Storico_G_df
# Selecting Column A,P from Storico_G+1 and stored in variable Storico_G1_df

Storico_G_df <- data.frame(Storico_G$pubblicazione,Storico_G$IMMESSO, Storico_G$`RICONSEGNATO.(1)`, Storico_G$BILANCIAMENTO.RESIDUALE )
Storico_G1_df <- data.frame(Storico_G1$pubblicazione, Storico_G1$`SBILANCIAMENTO.ATTESO.DEL.SISTEMA.(SAS)`)


# Conerting pubblicazione in date format and time
Storico_G_df$pubblicazione <- ymd_h(Storico_G_df$Storico_G.pubblicazione)
Storico_G1_df$pubblicazione   <- ymd_h(Storico_G1_df$Storico_G1.pubblicazione)


# Selecting on row which is having 4PM value in Storico_G+1 excel sheet tab
Storico_G1_df <- subset(Storico_G1_df, hour(Storico_G1_df$pubblicazione) == 16)
rownames(Storico_G1_df) <- 1:nrow(Storico_G1_df)

# Averaging hourly values to 1 daily data point in G excel sheet tab
Storico_G_df$Storico_G.pubblicazione <- strptime(Storico_G_df$Storico_G.pubblicazione, "%Y_%m_%d_%H")
storico_G_df_agg <- aggregate(Storico_G_df, by=list(day=format(Storico_G_df$Storico_G.pubblicazione, "%F")), FUN=mean, na.rm=TRUE)

初始問題:我難以解決以下問題:我有一個每小時的時間序列,其中已經包含特定時間的NA。 無論如何,我決定還為每個值(除了16:00以外)分配NA。 基本上,我只想使用一個數據打印,但仍保留時間戳,因為我需要與正常的每小時數據一起繪制(每天提供24個數據點)。

另外,我可以在每天的16:00繪制完整數據的每日平均值和數據點,以確保對齊。 顯然,這暗示着為整個時間序列創建每日平均值,並且僅對每天16:00的數據點進行過濾。

非常感謝我能解決我的小難題的任何幫助。

干杯

您的代碼不適用於xlsx包,因此我無法處理您的實際數據。 這是帶有偽造數據的可復制樣本。

d <- data.frame(time=paste0("2017_07_",rep(10:15, each=24),"_", 
                            formatC(0:23, flag="0", width=2)),
                value=cumsum(rnorm(24*6))  )

d$time <- strptime(d$time, "%Y_%m_%d_%H")

dagg <- aggregate(d, by=list(day=format(d$time, "%F")), FUN=mean, na.rm=TRUE)[,-2]
dagg$day <- strptime(dagg$day, format="%F")

plot(d, type="l", las=1)
lines(dagg, col=2)

另外,您的數據似乎混亂了,例如查看以下時間戳記:

2017_07_04_21
2017_07_04_22
2017_07_04_23
2017_07_04_00 <-- day 05?
2017_07_04_01
2017_07_04_02
2017_07_04_03
2017_07_04_04
2017_07_04_05
2017_07_05_06
2017_07_05_07

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM