Skip to contents

Reshape multi-dimensional arrays of a sector or case into two-dimensional tables, with optional stake summation and global multi-sector aggregation.

Usage

format_sector(x, what = "damage", add_total = TRUE)

Arguments

x

An object of class Sector or Case (or a list of Sector objects).

what

Character scalar specifying the theme array to reshape (default "damage"). Other valid themes include "scenario", "complement", etc.

add_total

Logical scalar (default TRUE), whether to add stake totals and global multi-sector aggregation.

Value

If x is a Sector: a data.frame with dimension identification columns and one column per flood event. If x is a Case: a named list of such data frames for each sector, including "global" if add_total = TRUE and multiple sectors exist.

Details

Sector objects contain multi-dimensional arrays for impact themes (such as "damage", "scenario", "complement"). This helper reshapes the array into a wide data frame where flood events form columns and other dimensions (state, scenario, stake) form rows.

  • If add_total = TRUE, a total row summing across stakes is appended for each combination of state and scenario.

  • If x is a Case (or list of sectors) with multiple sectors and add_total = TRUE, a "global" sector entry is computed by summing values across all sectors, provided flood events are consistent.

Author

Frédéric Grelot, frederic.grelot.1994_cran@m4x.org

Examples

case = import_case(
    system.file("extdata", "case", "complete.ods", package = "floodam.cba")
)
#> import Case from /tmp/RtmpvMoJJK/temp_libpath2b69f6f14b9e9/floodam.cba/extdata/case/complete.ods
#>  ... done
format_sector(case, what = "damage")
#> $sector_1
#>          stake state scenario min  med   max
#> 1     activity     1  failure   0 2000  4000
#> 2     activity     1   normal   0 1000  2000
#> 3     activity     2  failure   0 2000  4000
#> 4     activity     2   normal   0 1000  2000
#> 5     activity     3  failure   0 1000  2000
#> 6     activity     3   normal   0  500  1000
#> 7  agriculture     1  failure   0  500  1000
#> 8  agriculture     1   normal   0 1000  2000
#> 9  agriculture     2  failure   0  500  1000
#> 10 agriculture     2   normal   0 1000  2000
#> 11 agriculture     3  failure   0 1000  2000
#> 12 agriculture     3   normal   0 1500  3000
#> 13    dwelling     1  failure   0 2000  4000
#> 14    dwelling     1   normal   0 1000  2000
#> 15    dwelling     2  failure   0 2000  4000
#> 16    dwelling     2   normal   0 1000  2000
#> 17    dwelling     3  failure   0 2500  2500
#> 18    dwelling     3   normal   0 1200  1500
#> 19      public     1  failure   0 1000  2000
#> 20      public     1   normal   0 1000  2000
#> 21      public     2  failure   0 1000  2000
#> 22      public     2   normal   0 1000  2000
#> 23      public     3  failure   0 1000  2000
#> 24      public     3   normal   0 1000  2000
#> 25       total     1  failure   0 5500 11000
#> 26       total     1   normal   0 4000  8000
#> 27       total     2  failure   0 5500 11000
#> 28       total     2   normal   0 4000  8000
#> 29       total     3  failure   0 5500  8500
#> 30       total     3   normal   0 4200  7500
#> 
#> $sector_2
#>          stake state scenario min  med   max
#> 1     activity     1  failure   0 2000  4000
#> 2     activity     1   normal   0 1000  2000
#> 3     activity     2  failure   0 2000  4000
#> 4     activity     2   normal   0 1000  2000
#> 5     activity     3  failure   0 1000  2000
#> 6     activity     3   normal   0  500  1000
#> 7  agriculture     1  failure   0  500  1000
#> 8  agriculture     1   normal   0 1000  2000
#> 9  agriculture     2  failure   0  500  1000
#> 10 agriculture     2   normal   0 1000  2000
#> 11 agriculture     3  failure   0 1000  2000
#> 12 agriculture     3   normal   0 1500  3000
#> 13    dwelling     1  failure   0 2000  4000
#> 14    dwelling     1   normal   0 1000  2000
#> 15    dwelling     2  failure   0 2000  4000
#> 16    dwelling     2   normal   0 1000  2000
#> 17    dwelling     3  failure   0 2500  2500
#> 18    dwelling     3   normal   0 1200  1500
#> 19      public     1  failure   0 1000  2000
#> 20      public     1   normal   0 1000  2000
#> 21      public     2  failure   0 1000  2000
#> 22      public     2   normal   0 1000  2000
#> 23      public     3  failure   0 1000  2000
#> 24      public     3   normal   0 1000  2000
#> 25       total     1  failure   0 5500 11000
#> 26       total     1   normal   0 4000  8000
#> 27       total     2  failure   0 5500 11000
#> 28       total     2   normal   0 4000  8000
#> 29       total     3  failure   0 5500  8500
#> 30       total     3   normal   0 4200  7500
#> 
#> $sector_3
#>          stake state scenario min  med   max
#> 1     activity     1  failure   0 2000  4000
#> 2     activity     1   normal   0 1000  2000
#> 3     activity     2  failure   0 2000  4000
#> 4     activity     2   normal   0 1000  2000
#> 5     activity     3  failure   0 1000  2000
#> 6     activity     3   normal   0  500  1000
#> 7  agriculture     1  failure   0  500  1000
#> 8  agriculture     1   normal   0 1000  2000
#> 9  agriculture     2  failure   0  500  1000
#> 10 agriculture     2   normal   0 1000  2000
#> 11 agriculture     3  failure   0 1000  2000
#> 12 agriculture     3   normal   0 1500  3000
#> 13    dwelling     1  failure   0 2000  4000
#> 14    dwelling     1   normal   0 1000  2000
#> 15    dwelling     2  failure   0 2000  4000
#> 16    dwelling     2   normal   0 1000  2000
#> 17    dwelling     3  failure   0 2500  2500
#> 18    dwelling     3   normal   0 1200  1500
#> 19      public     1  failure   0 1000  2000
#> 20      public     1   normal   0 1000  2000
#> 21      public     2  failure   0 1000  2000
#> 22      public     2   normal   0 1000  2000
#> 23      public     3  failure   0 1000  2000
#> 24      public     3   normal   0 1000  2000
#> 25       total     1  failure   0 5500 11000
#> 26       total     1   normal   0 4000  8000
#> 27       total     2  failure   0 5500 11000
#> 28       total     2   normal   0 4000  8000
#> 29       total     3  failure   0 5500  8500
#> 30       total     3   normal   0 4200  7500
#> 
#> $global
#>          stake state scenario min   med   max
#> 1     activity     1  failure   0  6000 12000
#> 2     activity     1   normal   0  3000  6000
#> 3     activity     2  failure   0  6000 12000
#> 4     activity     2   normal   0  3000  6000
#> 5     activity     3  failure   0  3000  6000
#> 6     activity     3   normal   0  1500  3000
#> 7  agriculture     1  failure   0  1500  3000
#> 8  agriculture     1   normal   0  3000  6000
#> 9  agriculture     2  failure   0  1500  3000
#> 10 agriculture     2   normal   0  3000  6000
#> 11 agriculture     3  failure   0  3000  6000
#> 12 agriculture     3   normal   0  4500  9000
#> 13    dwelling     1  failure   0  6000 12000
#> 14    dwelling     1   normal   0  3000  6000
#> 15    dwelling     2  failure   0  6000 12000
#> 16    dwelling     2   normal   0  3000  6000
#> 17    dwelling     3  failure   0  7500  7500
#> 18    dwelling     3   normal   0  3600  4500
#> 19      public     1  failure   0  3000  6000
#> 20      public     1   normal   0  3000  6000
#> 21      public     2  failure   0  3000  6000
#> 22      public     2   normal   0  3000  6000
#> 23      public     3  failure   0  3000  6000
#> 24      public     3   normal   0  3000  6000
#> 25       total     1  failure   0 16500 33000
#> 26       total     1   normal   0 12000 24000
#> 27       total     2  failure   0 16500 33000
#> 28       total     2   normal   0 12000 24000
#> 29       total     3  failure   0 16500 25500
#> 30       total     3   normal   0 12600 22500
#> 
format_sector(case["sector"][[1]], what = "scenario", add_total = FALSE)
#>   state scenario min    med   max
#> 1     1  failure   0 0.1000 1.000
#> 2     1   normal   1 0.9000 0.000
#> 3     2  failure   0 0.0001 0.001
#> 4     2   normal   1 0.9999 0.999
#> 5     3  failure   0 0.0001 0.001
#> 6     3   normal   1 0.9999 0.999