macroAggregateBin | R Documentation |
Aggregate simulation results by some date subsets, using various functions. macroAggregateBin can be used on a data.frame containing simulation results (or weather data or any time series data) to compute some aggregation function (FUN = sum, mean, median, ...) over subsets of dates (aggregate by day, week, month, ...).
macroAggregateBin(
x,
columns = colnames(x),
by = "%Y-%m-%d",
FUN = sum,
dateCol = "Date"
)
x |
A data.frame, with a date column named after 'dateCol' (default "Date") and one or several variables to be aggregated on a certain time interval (defined in 'by'). The column 'dateCol' must be in POSIXct format. |
columns |
A vector of character strings. Name of the columns to be selected and aggregated. 'dateCol' does not need to be specified here, but can be included. |
by |
A character string representing a POSIXct format (see ?format.POSIXct). "%Y-%m-%d" (the default) will aggregate the data by days and "%Y-%m-%d %H", "%Y-%W", "%Y-%m" will aggregate the data by hour, week of the year or month, respectively. Other combinations are possible. |
FUN |
A function to be applied to aggregate the data on each element of 'by'. Can be 'sum', 'mean', 'median', etc. For removing missing values, choose something like 'function(x)sum(x,na.rm=TRUE)'. Another possibility would be 'function(x)quantile(x,probs=.75)'. The same function is applied for all columns, so consider applying different macroAggregateBin() on different data types if needed. |
dateCol |
Name of the column containing the POSIXct date values. Default is 'Date'. |
Returns a data.frame with the values in columns aggregated by 'by' with the function 'FUN'. Notice that the format of 'dateCol' is then "character", and not any more POSIXct (because no uniform date format are possible for exporting back the dates).
library( "macroutils2" )
# ====== Read a bin file ======
# Format the path to a test binary file
( filenm <- system.file(
"bintest/chat_winCer_GW-D_1kgHa_d298_annual_output.bin",
package = "macroutils2", mustWork = TRUE ) )
# Read the binary file
tmp1 <- macroReadBin( f = filenm )
# Inspect the table
colnames( tmp1 )
dim( tmp1 )
# ====== Aggregate the results =====
# Mean by year and month (only top results shown):
head( r1 <- macroAggregateBin( x = tmp1, by = "%Y-%m", FUN = mean ) )
# Mean by month too, but on one column only
# (only top results shown):
head( r2 <- macroAggregateBin(
x = tmp1,
columns = "CCET",
by = "%Y-%m",
FUN = mean ) )
# Mean by week of year (00 -> 53):
r3 <- macroAggregateBin( x = tmp1, by = "%Y-%W", FUN = mean )
# Inspect the results
head( r3 )
tail( r3 )
# Notice the new format of the column 'Date' ("character")
class( r1[,"Date"] )
# Trick to convert r1$Date to POSIXct date again, by adding a virtual day:
r1[,"Date"] <- as.POSIXct( paste( sep = "", r1[,"Date"], "-15" ),
format = "%Y-%m-%d", tz = "GMT" )
class( r1[,"Date"] )
# Plot the results
plot( r1[,2] ~ r1[,"Date"], type = "b", col = "red" )
# ====== using the original R aggregate() =====
# More code, but a bit faster
r1b <- aggregate(
x = tmp1[,-1],
by = list( "Date" = format.POSIXct( tmp1[,"Date"], "%Y-%m" ) ),
FUN = mean )
head( r1b )
identical( r1[,-1], r1b[,-1] )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.