R-每小时时间序列上特定小时的NAs

R-每小时时间序列上特定小时的NAs,r,time-series,R,Time Series,编辑:我遇到了另一个问题,因此编辑了这个问题: 一方面将每小时的数据聚合为每日平均值,另一方面每天过滤1个数据点(16:00),我得到了相同数量的数据点(每天1个)。然而,由于我想要包含数据帧,如果我在16:00获得数据点之前运行代码,我将不会有同样多的行。因此,如果还没有可用的数据点,我想添加一行(带有日期和NA值)。我添加了代码,那么它应该是有意义的 is.installed <- function(mypkg){ is.element(mypkg, installed.packa

编辑:我遇到了另一个问题,因此编辑了这个问题: 一方面将每小时的数据聚合为每日平均值,另一方面每天过滤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)

已安装您的代码与软件包
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)

d你好,贝瑞,非常感谢!我用的是OPENXLSX,很抱歉应该加上这个。需要看一下时间戳。。干杯
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