macroAggregateBin {macroutils}R Documentation

Aggregate simulation results by some date subsets, using various functions

Description

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

Usage

macroAggregateBin(x, columns = colnames(x), by = "%Y-%m-%d", FUN = sum,
  dateCol = "Date")

Arguments

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 charcater 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 cotaining the POSIXct date values. Default is 'Date'.

Value

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 exporying back the dates).

Examples

library( "macroutils" )



# ====== Read a bin file ======

#   Format the path to a test binary file
( filenm <- system.file( "bintest/METFILE.BIN", 
    package = "macroutils", mustWork = TRUE ) ) 
## [1] "C:/Users/julienm/Documents/R/win-library/3.2/macroutils/bintest/METFILE.BIN"
#   Read the binary file
tmp1 <- macroReadBin( file = filenm ) 

#   Inspect the table
colnames( tmp1 ) 
## [1] "Date"                                       
## [2] "Max_air_temperature_oC_Sutton_Bonington"    
## [3] "Min_air_temperature_oC_Sutton_Bonington"    
## [4] "Solar_radiation_W_m_2_Sutton_Bonington_TRS1"
## [5] "Vapour_pressure_kPa_Sutton_Bonington_TRS1"  
## [6] "Wind_speed_m_s_1_Sutton_Bonington_TRS1"
dim( tmp1 ) 
## [1] 731   6
# ====== Aggregate the results =====

#   Sum by year and month:
( r1 <- macroAggregateBin( x = tmp1, by = "%Y-%m", FUN = sum ) )
##       Date Max_air_temperature_oC_Sutton_Bonington
## 1  1987-01                                    96.9
## 2  1987-02                                   190.1
## 3  1987-03                                   232.7
## 4  1987-04                                   447.1
## 5  1987-05                                   444.0
## 6  1987-06                                   498.4
## 7  1987-07                                   621.7
## 8  1987-08                                   615.4
## 9  1987-09                                   538.2
## 10 1987-10                                   410.2
## 11 1987-11                                   255.9
## 12 1987-12                                   250.6
## 13 1988-01                                   248.1
## 14 1988-02                                   230.3
## 15 1988-03                                   304.9
## 16 1988-04                                   354.5
## 17 1988-05                                   495.3
## 18 1988-06                                   553.5
## 19 1988-07                                   572.3
## 20 1988-08                                   615.9
## 21 1988-09                                   525.6
## 22 1988-10                                   431.8
## 23 1988-11                                   261.8
## 24 1988-12                                   308.6
##    Min_air_temperature_oC_Sutton_Bonington
## 1                                    -59.8
## 2                                     20.9
## 3                                     19.4
## 4                                    159.5
## 5                                    167.1
## 6                                    257.6
## 7                                    355.3
## 8                                    365.7
## 9                                    281.7
## 10                                   178.5
## 11                                   121.1
## 12                                    94.7
## 13                                    83.6
## 14                                    51.8
## 15                                    89.8
## 16                                   120.3
## 17                                   200.3
## 18                                   289.5
## 19                                   360.7
## 20                                   344.3
## 21                                   291.7
## 22                                   219.9
## 23                                    23.8
## 24                                   140.7
##    Solar_radiation_W_m_2_Sutton_Bonington_TRS1
## 1                                     827.7796
## 2                                    1480.9061
## 3                                    2619.1030
## 4                                    3401.2806
## 5                                    5278.5996
## 6                                    4475.9358
## 7                                    5170.8447
## 8                                    4028.4811
## 9                                    3631.2580
## 10                                   2043.0601
## 11                                    730.3257
## 12                                    506.5983
## 13                                    833.3352
## 14                                   1800.1197
## 15                                   2700.0059
## 16                                   3689.1285
## 17                                   5515.0584
## 18                                   5373.5073
## 19                                   5059.7334
## 20                                   5059.8491
## 21                                   3270.4933
## 22                                   1902.3190
## 23                                    999.5392
## 24                                    589.8161
##    Vapour_pressure_kPa_Sutton_Bonington_TRS1
## 1                                      18.06
## 2                                      19.69
## 3                                      21.39
## 4                                      30.32
## 5                                      30.18
## 6                                      36.87
## 7                                      43.27
## 8                                      46.15
## 9                                      37.64
## 10                                     32.30
## 11                                     25.12
## 12                                     25.32
## 13                                     24.17
## 14                                     19.49
## 15                                     24.01
## 16                                     26.06
## 17                                     32.11
## 18                                     38.46
## 19                                     41.02
## 20                                     40.54
## 21                                     35.28
## 22                                     33.59
## 23                                     23.02
## 24                                     27.65
##    Wind_speed_m_s_1_Sutton_Bonington_TRS1
## 1                                67.54072
## 2                                68.92960
## 3                                99.02200
## 4                                78.49744
## 5                                80.45216
## 6                                61.67656
## 7                                63.63128
## 8                                59.92760
## 9                                69.49544
## 10                               72.32464
## 11                               72.99336
## 12                               92.02616
## 13                              101.69688
## 14                              102.15984
## 15                               86.57352
## 16                               79.93776
## 17                               70.26704
## 18                               63.99136
## 19                               90.48296
## 20                               81.84104
## 21                               78.29168
## 22                               76.90280
## 23                               48.61080
## 24                               92.23192
#   Sum by month too, but on one column only:
( r2 <- macroAggregateBin( 
    x       = tmp1, 
    columns = "Wind_speed_m_s_1_Sutton_Bonington_TRS1", 
    by      = "%Y-%m", 
    FUN     = sum ) ) 
##       Date Wind_speed_m_s_1_Sutton_Bonington_TRS1
## 1  1987-01                               67.54072
## 2  1987-02                               68.92960
## 3  1987-03                               99.02200
## 4  1987-04                               78.49744
## 5  1987-05                               80.45216
## 6  1987-06                               61.67656
## 7  1987-07                               63.63128
## 8  1987-08                               59.92760
## 9  1987-09                               69.49544
## 10 1987-10                               72.32464
## 11 1987-11                               72.99336
## 12 1987-12                               92.02616
## 13 1988-01                              101.69688
## 14 1988-02                              102.15984
## 15 1988-03                               86.57352
## 16 1988-04                               79.93776
## 17 1988-05                               70.26704
## 18 1988-06                               63.99136
## 19 1988-07                               90.48296
## 20 1988-08                               81.84104
## 21 1988-09                               78.29168
## 22 1988-10                               76.90280
## 23 1988-11                               48.61080
## 24 1988-12                               92.23192
#   Sum by week of year (00 -> 53):
r3 <- macroAggregateBin( x = tmp1, by = "%Y-%W", FUN = sum ) 

#   Inspect the results
head( r3 ) 
##      Date Max_air_temperature_oC_Sutton_Bonington
## 1 1987-00                                    31.9
## 2 1987-01                                    16.4
## 3 1987-02                                   -10.3
## 4 1987-03                                    38.8
## 5 1987-04                                    24.0
## 6 1987-05                                    64.8
##   Min_air_temperature_oC_Sutton_Bonington
## 1                                     4.1
## 2                                    -8.5
## 3                                   -42.1
## 4                                     0.9
## 5                                   -21.3
## 6                                    21.4
##   Solar_radiation_W_m_2_Sutton_Bonington_TRS1
## 1                                    72.91683
## 2                                   229.16717
## 3                                   135.41697
## 4                                   122.68546
## 5                                   330.09332
## 6                                   251.15797
##   Vapour_pressure_kPa_Sutton_Bonington_TRS1
## 1                                      3.01
## 2                                      3.60
## 3                                      3.14
## 4                                      5.10
## 5                                      3.62
## 6                                      5.94
##   Wind_speed_m_s_1_Sutton_Bonington_TRS1
## 1                               13.16864
## 2                               17.84968
## 3                               24.99984
## 4                                6.68720
## 5                                6.84152
## 6                               19.34144
tail( r3 ) 
##        Date Max_air_temperature_oC_Sutton_Bonington
## 101 1988-47                                    37.3
## 102 1988-48                                    58.9
## 103 1988-49                                    67.4
## 104 1988-50                                    64.7
## 105 1988-51                                    79.3
## 106 1988-52                                    69.4
##     Min_air_temperature_oC_Sutton_Bonington
## 101                                   -17.6
## 102                                    16.8
## 103                                    21.2
## 104                                    29.5
## 105                                    42.9
## 106                                    37.6
##     Solar_radiation_W_m_2_Sutton_Bonington_TRS1
## 101                                   217.13011
## 102                                    98.61133
## 103                                   178.58836
## 104                                   122.45397
## 105                                   108.56505
## 106                                   128.00954
##     Vapour_pressure_kPa_Sutton_Bonington_TRS1
## 101                                      4.05
## 102                                      5.86
## 103                                      5.56
## 104                                      6.19
## 105                                      6.69
## 106                                      6.12
##     Wind_speed_m_s_1_Sutton_Bonington_TRS1
## 101                                7.30448
## 102                               17.23240
## 103                               20.93608
## 104                               11.77976
## 105                               30.19528
## 106                               18.77560
#   Notice the new format of the column 'Date' ("character")
class( r1[,"Date"] )
## [1] "character"
#   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 = getMuPar( "tz" ) ) 
class( r1[,"Date"] )
## [1] "POSIXct" "POSIXt"
#   Plot the results
plot( r1[,2] ~ r1[,"Date"], type = "b", col = "red" ) 

plot of chunk unnamed-chunk-1

# ====== 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 = sum ) 
r1b 
##       Date Max_air_temperature_oC_Sutton_Bonington
## 1  1987-01                                    96.9
## 2  1987-02                                   190.1
## 3  1987-03                                   232.7
## 4  1987-04                                   447.1
## 5  1987-05                                   444.0
## 6  1987-06                                   498.4
## 7  1987-07                                   621.7
## 8  1987-08                                   615.4
## 9  1987-09                                   538.2
## 10 1987-10                                   410.2
## 11 1987-11                                   255.9
## 12 1987-12                                   250.6
## 13 1988-01                                   248.1
## 14 1988-02                                   230.3
## 15 1988-03                                   304.9
## 16 1988-04                                   354.5
## 17 1988-05                                   495.3
## 18 1988-06                                   553.5
## 19 1988-07                                   572.3
## 20 1988-08                                   615.9
## 21 1988-09                                   525.6
## 22 1988-10                                   431.8
## 23 1988-11                                   261.8
## 24 1988-12                                   308.6
##    Min_air_temperature_oC_Sutton_Bonington
## 1                                    -59.8
## 2                                     20.9
## 3                                     19.4
## 4                                    159.5
## 5                                    167.1
## 6                                    257.6
## 7                                    355.3
## 8                                    365.7
## 9                                    281.7
## 10                                   178.5
## 11                                   121.1
## 12                                    94.7
## 13                                    83.6
## 14                                    51.8
## 15                                    89.8
## 16                                   120.3
## 17                                   200.3
## 18                                   289.5
## 19                                   360.7
## 20                                   344.3
## 21                                   291.7
## 22                                   219.9
## 23                                    23.8
## 24                                   140.7
##    Solar_radiation_W_m_2_Sutton_Bonington_TRS1
## 1                                     827.7796
## 2                                    1480.9061
## 3                                    2619.1030
## 4                                    3401.2806
## 5                                    5278.5996
## 6                                    4475.9358
## 7                                    5170.8447
## 8                                    4028.4811
## 9                                    3631.2580
## 10                                   2043.0601
## 11                                    730.3257
## 12                                    506.5983
## 13                                    833.3352
## 14                                   1800.1197
## 15                                   2700.0059
## 16                                   3689.1285
## 17                                   5515.0584
## 18                                   5373.5073
## 19                                   5059.7334
## 20                                   5059.8491
## 21                                   3270.4933
## 22                                   1902.3190
## 23                                    999.5392
## 24                                    589.8161
##    Vapour_pressure_kPa_Sutton_Bonington_TRS1
## 1                                      18.06
## 2                                      19.69
## 3                                      21.39
## 4                                      30.32
## 5                                      30.18
## 6                                      36.87
## 7                                      43.27
## 8                                      46.15
## 9                                      37.64
## 10                                     32.30
## 11                                     25.12
## 12                                     25.32
## 13                                     24.17
## 14                                     19.49
## 15                                     24.01
## 16                                     26.06
## 17                                     32.11
## 18                                     38.46
## 19                                     41.02
## 20                                     40.54
## 21                                     35.28
## 22                                     33.59
## 23                                     23.02
## 24                                     27.65
##    Wind_speed_m_s_1_Sutton_Bonington_TRS1
## 1                                67.54072
## 2                                68.92960
## 3                                99.02200
## 4                                78.49744
## 5                                80.45216
## 6                                61.67656
## 7                                63.63128
## 8                                59.92760
## 9                                69.49544
## 10                               72.32464
## 11                               72.99336
## 12                               92.02616
## 13                              101.69688
## 14                              102.15984
## 15                               86.57352
## 16                               79.93776
## 17                               70.26704
## 18                               63.99136
## 19                               90.48296
## 20                               81.84104
## 21                               78.29168
## 22                               76.90280
## 23                               48.61080
## 24                               92.23192
identical( r1[,-1], r1b[,-1] )
## [1] TRUE

[Package macroutils version 1.8.2 Index]