计算多个数据集的日、月和年平均值 [英] Compute daily, month and annual average of several data sets

查看:31
本文介绍了计算多个数据集的日、月和年平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框:

MS_NR SS_NR 日期小时值1 13095010 68 1/01/2014 0:00:00 9,82 13095010 68 1/01/2014 1:00:00 8,03 13095010 68 1/01/2014 2:00:00 不适用4 13095010 68 1/01/2014 3:00:00 7,55 13095010 68 1/01/2014 4:00:00 7,06 13095010 68 1/01/2014 5:00:00 8,5

是每小时对气象站进行的温度观测,我想计算不同气象站的几个数据框的日、周、月和年平均值.如何在循环中执行此操作,以便该过程不重复?

解决方案

处理水文气象数据时,我通常使用 (v0.2.0) 于 2018 年 2 月 28 日创建.

I have a data frame:

MS_NR SS_NR      DATE       HOUR     VALUE
1 13095010    68 1/01/2014 0:00:00    9,8
2 13095010    68 1/01/2014 1:00:00    8,0
3 13095010    68 1/01/2014 2:00:00    NA
4 13095010    68 1/01/2014 3:00:00    7,5
5 13095010    68 1/01/2014 4:00:00    7,0
6 13095010    68 1/01/2014 5:00:00    8,5

are temperature observations of a weather station taken every hour, I want to calculate the daily, weekly, monthly and annual averages of several data frames of different weather stations. How can I do this within a loop, so that the process is not repetitive?

解决方案

When working with hydro-meteorological data, I usually use xts and hydroTSM packages as they have many functions for data aggregation.

You didn't provide any data so I created one for demonstration purpose

library(xts)
library(hydroTSM)

# Generate random data
set.seed(2018)
date = seq(from = as.Date("2016-01-01"), to = as.Date("2018-12-31"),
           by = "days")
temperature = runif(length(date), -15, 35)
dat <- data.frame(date, temperature)

# Convert to xts object for xts & hydroTSM functions
dat_xts <- xts(dat[, -1], order.by = dat$date)

# All daily, monthly & annual series in one plot
hydroplot(dat_xts, pfreq = "dma", var.type = "Temperature")

# Weekly average
dat_weekly <- apply.weekly(dat_xts, FUN = mean)
plot(dat_weekly)

# Monthly average
dat_monthly <- daily2monthly(dat_xts, FUN = mean, na.rm = TRUE)
plot.zoo(dat_monthly, xaxt = "n", xlab = "")
axis.Date(1, at = pretty(index(dat_monthly)),
          labels = format(pretty(index(dat_monthly)), format = "%b-%Y"),
          las = 1, cex.axis = 1.1)

# Seasonal average: need to specify the months
dat_seasonal <- dm2seasonal(dat_xts, season = "DJF", FUN = mean, na.rm = TRUE)
plot(dat_seasonal)

# Annual average
dat_annual <- daily2annual(dat_xts, FUN = mean, na.rm = TRUE)
plot(dat_annual)

Edit: using OP's data

df <- readr::read_csv2("Temp_2014_Hour.csv")
str(df)

# Convert DATE to Date object & put in a new column
df$date <- as.Date(df$DATE, format = "%d/%m/%Y")
dat <- df[, c("date", "VALUE")]
str(dat)

dat_xts <- xts(dat[, -1], order.by = dat$date)

Created on 2018-02-28 by the reprex package (v0.2.0).

这篇关于计算多个数据集的日、月和年平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆