1

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?

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 2
    Are the values i.e. VALUE really like "9,8" and not 9.8? – Stephen Henderson Feb 28 '18 at 20:35
  • 1
    @Antonio Bonilla: please use `dput` to share your data https://meta.stackoverflow.com/questions/315885/what-is-the-correct-way-to-share-r-data-on-stackoverflow or upload your file to a site such as http://www.sharecsv.com/ – Tung Feb 28 '18 at 20:40
  • http://www.sharecsv.com/s/39bd55fe0af797c524920bbb4ce9ecdc/Temp_2014_Hour.csv here is file csv, and @Stephen Henderson yes the values really like "9,8" – Antonio Bonilla Mar 01 '18 at 14:47
  • hello I am starting to program in R, my knowledge is basic, but I have tried something like this with the openair package – Antonio Bonilla Mar 01 '18 at 14:55

2 Answers2

1

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).

Tung
  • 26,371
  • 7
  • 91
  • 115
  • thank you very much for your contribution, I upload my data frame, but at the moment of converting to object xts the message comes out "order.by requires an appropiate time-based object" – Antonio Bonilla Mar 01 '18 at 14:53
  • well, creating a new column if it works, now when you have to repeat the procedure for many data frames, is it possible to create a function?, thanks – Antonio Bonilla Mar 01 '18 at 20:08
  • Yes! You just need to put everything (before the conversion to `xts` object) in a function e.g. `time_series_aggregation <- function(dataFrame, varType) {...}` then call it `time_series_aggregation(dataFrame = dat_df, "Temperature")` – Tung Mar 02 '18 at 18:29
1

I try this

first using read.table load the file

library(openair)

Temp <- read.table (file, header=TRUE, sep=";",stringsAsFactors = FALSE, dec = ",", na.strings = "NA")

tiempos <- Temp$HOUR
timestamps <- as.POSIXlt(as.POSIXct('1900-1-1', tz='UTC') 
                         + as.difftime(as.character(tiempos))
time <- format(timestamps, format='%H:%M:%S')
date<-paste(Temp[,3], time, sep=" ")
date

Temp_met <- cbind(date, CovTemp[-c(3,4)])
Temp_met$date <- as.POSIXct(strptime(Met_CovTemp$date,
                                                  format = "%d/%m/%Y %H:%M", "GMT"))

## daily mean
Temp_daily <- timeAverage(Met_CovTemp, avg.time = "day")
## weekly mean
Temp_week <- timeAverage(Met_CovTemp, avg.time = "week")
## monthly mean
Temp_month <- timeAverage(Met_CovTemp, avg.time = "month")
## annual mean
Temp_annual <- timeAverage(Met_CovTemp, avg.time = "year")