我有以下格式的每小时风速数据
DT,DIR,SPEED
2002/01/01 00:00,***,0.0
2002/01/01 03:00,360,0.89408
2002/01/01 06:00,070,4.91744
2002/01/01 09:00,050,4.4704
2002/01/01 15:00,050,2.2352
2002/01/01 18:00,050,3.12928
2002/01/01 21:00,020,0.89408
从数据点开始,在2002年至2012年的一个小时内,在三个小时内记录三次到数据点,如下所示:
2012/12/31 00:00,***,0.0
2012/12/31 00:10,***,0.0
2012/12/31 00:40,***,0.0
2012/12/31 01:10,***,0.0
2012/12/31 01:40,***,0.0
2012/12/31 02:10,***,0.0
2012/12/31 02:40,***,0.0
2012/12/31 03:00,***,0.0
2012/12/31 03:10,310,2.2352
2012/12/31 03:40,060,4.02336
2012/12/31 04:40,060,3.12928
2012/12/31 05:10,070,4.91744
我正在尝试使用R创建显示SPEED与No.of.Hours的年度频率图。我尝试使用直方图,但点数不相等,当然也不完全代表no.of小时。怎么解决这个问题?
注意:不使用DIR值, * 也被视为NA
答案 0 :(得分:0)
您可以使用approx()
函数估算每小时的速度,然后使用这些估计的每小时速度来创建直方图。例如,假设您的数据框名为df
,...
library(lubridate)
# date/time as class POSIXct
df$DT2 <- ymd_hm(df$DT)
# create a new data frame, everyhour, with every hour between the first and the last in df
everyhour <- data.frame(DT2=seq(ceiling_date(min(df$DT2), "hour"), floor_date(max(df$DT2), "hour"), 3600), FORHIST=TRUE)
# merge the observed data with the everyhour data
df2 <- merge(df, everyhour, all=TRUE)
# set missing FORHIST to FALSE
df2$FORHIST[is.na(df2$FORHIST)] <- FALSE
# define year
df2$YEAR <- year(df2$DT2)
# estimate speed for everyhour
df2$estSPEED <- approx(x=df2$DT2, y=df2$SPEED, xout=df2$DT2, method="linear")$y
# plot annual histograms of hourly speeds
suy <- sort(unique(df2$YEAR))
par(mfrow=n2mfrow(length(suy)), mar=c(3, 3, 2, 1), oma=c(2, 2, 0, 0))
for(i in seq(suy)) {
sel <- df2$YEAR==suy[i] & df2$FORHIST==TRUE
hist(df2$estSPEED[sel], xlab="", ylab="", main=suy[i])
}
mtext("Speed", side=1, outer=TRUE)
mtext("Frequency", side=2, outer=TRUE)