Group objects over some index by applying a function over a single dimension
Source:R/genericMethods.R
, R/FLQuant.R
group.Rd
Array objects (e.g. FLQuant or FLQuants) are divided along a single dimnension following a given index or expression, an aggregating function is applied to each subset, and the results are joined again. Data can be added, for example, by decade or for two age groups.
Arguments
- x
An object to group.
- FUN
A function to apply along the chosen dimension, defaults to 'sum'.
- ...
An expression or indexing vector, named as the chosen dimension. Extra arguments to FUN can also be provided, but cannmot match names in x.
Examples
data(ple4)
# Add catch-at-age along two age groups, 'juv'eniles and 'adu'lts
group(catch.n(ple4), sum, age=c('juv', 'juv', rep('adu', 8)))
#> An object of class "FLQuant"
#> , , unit = unique, season = all, area = unique
#>
#> year
#> age 1957 1958 1959 1960 1961 1962 1963 1964 1965
#> juv 82844 144028 220590 250246 240235 212994 163538 284592 488725
#> adu 217709 216081 229525 263171 300448 323824 375336 356155 320567
#> year
#> age 1966 1967 1968 1969 1970 1971 1972 1973 1974
#> juv 150830 136276 135024 197136 251958 200146 144800 322925 524851
#> adu 634678 479953 396629 324414 263583 266412 285149 264431 221214
#> year
#> age 1975 1976 1977 1978 1979 1980 1981 1982 1983
#> juv 424841 354998 453126 483173 435736 446896 455098 628425 775178
#> adu 349358 352906 316769 286774 315207 303644 303591 333781 335564
#> year
#> age 1984 1985 1986 1987 1988 1989 1990 1991 1992
#> juv 645330 763038 1500360 1489939 853142 683898 503947 446701 410487
#> adu 501451 501309 484611 554359 1016274 795393 685345 560852 485903
#> year
#> age 1993 1994 1995 1996 1997 1998 1999 2000 2001
#> juv 316124 221653 255472 337605 528114 793612 278299 267005 285233
#> adu 426742 361297 282531 268988 366551 360498 637135 443016 365948
#> year
#> age 2002 2003 2004 2005 2006 2007 2008 2009 2010
#> juv 445588 623612 382435 443476 323632 368491 415136 344764 349579
#> adu 375610 288089 418011 247371 293727 259470 236503 280668 288770
#> year
#> age 2011 2012 2013 2014 2015 2016 2017
#> juv 339133 303843 306544 418660 354316 242102 258717
#> adu 310395 382591 449713 437356 442764 469107 397382
#>
#> units: 1000
# An expression can use based on dimnames
group(catch.n(ple4), sum, age=age < 3)
#> An object of class "FLQuant"
#> , , unit = unique, season = all, area = unique
#>
#> year
#> age 1957 1958 1959 1960 1961 1962 1963 1964 1965
#> TRUE 82844 144028 220590 250246 240235 212994 163538 284592 488725
#> FALSE 217709 216081 229525 263171 300448 323824 375336 356155 320567
#> year
#> age 1966 1967 1968 1969 1970 1971 1972 1973 1974
#> TRUE 150830 136276 135024 197136 251958 200146 144800 322925 524851
#> FALSE 634678 479953 396629 324414 263583 266412 285149 264431 221214
#> year
#> age 1975 1976 1977 1978 1979 1980 1981 1982 1983
#> TRUE 424841 354998 453126 483173 435736 446896 455098 628425 775178
#> FALSE 349358 352906 316769 286774 315207 303644 303591 333781 335564
#> year
#> age 1984 1985 1986 1987 1988 1989 1990 1991 1992
#> TRUE 645330 763038 1500360 1489939 853142 683898 503947 446701 410487
#> FALSE 501451 501309 484611 554359 1016274 795393 685345 560852 485903
#> year
#> age 1993 1994 1995 1996 1997 1998 1999 2000 2001
#> TRUE 316124 221653 255472 337605 528114 793612 278299 267005 285233
#> FALSE 426742 361297 282531 268988 366551 360498 637135 443016 365948
#> year
#> age 2002 2003 2004 2005 2006 2007 2008 2009 2010
#> TRUE 445588 623612 382435 443476 323632 368491 415136 344764 349579
#> FALSE 375610 288089 418011 247371 293727 259470 236503 280668 288770
#> year
#> age 2011 2012 2013 2014 2015 2016 2017
#> TRUE 339133 303843 306544 418660 354316 242102 258717
#> FALSE 310395 382591 449713 437356 442764 469107 397382
#>
#> units: 1000
# Mean by lustrum, by using 'year - year %% 5'
group(catch.n(ple4), mean, year = year - year %% 5)
#> An object of class "FLQuant"
#> , , unit = unique, season = all, area = unique
#>
#> year
#> age 1955 1960 1965 1970 1975 1980 1985
#> 1 71809.28 97610.97 56284.64 142896.67 236660.46 287021.52 522676.11
#> 2 77344.61 132710.09 165313.63 146039.20 193714.22 303163.94 535399.28
#> 3 70667.84 134317.60 177299.86 83950.61 151557.10 200469.06 364165.16
#> 4 53897.38 77484.89 102811.81 54227.97 77645.40 79912.77 183142.05
#> 5 37029.27 42708.80 65547.37 35383.39 42043.43 32622.44 67188.57
#> 6 18875.79 26412.29 41723.43 22399.67 22713.38 15434.63 29589.46
#> 7 11646.69 13542.50 14706.31 21814.34 10821.45 9640.98 12151.84
#> 8 8068.86 8236.27 8886.16 13606.39 4789.93 6739.11 5394.17
#> 9 6492.95 5485.61 5790.72 9201.28 3279.71 3588.63 2705.11
#> 10 14426.07 15598.83 14482.59 19574.23 11352.33 7198.61 6052.75
#> year
#> age 1990 1995 2000 2005 2010 2015
#> 1 151861.64 137447.51 165348.82 166915.67 161601.51 123926.27
#> 2 227920.56 301172.80 235425.77 212184.16 181950.43 161118.88
#> 3 214340.28 240942.90 187281.98 136159.64 163859.80 161994.17
#> 4 122344.54 71372.58 106119.37 68691.06 108733.54 123839.27
#> 5 88976.98 33588.09 50224.62 30263.20 53585.84 73302.27
#> 6 44095.10 17259.15 21675.79 15752.57 24526.45 39482.76
#> 7 17571.71 8772.14 7917.82 7192.23 11668.61 18795.52
#> 8 7692.84 4637.68 2477.50 2995.50 4801.66 7518.12
#> 9 4034.36 2399.99 747.03 962.01 2072.67 3159.34
#> 10 4971.86 4168.02 1690.74 1531.73 4516.31 8326.26
#>
#> units: 1000