groupAggregate: groupAggregate

View source: R/groupAggregate.r

groupAggregateR Documentation

groupAggregate

Description

Function which applies aggregation functions over a subset of an array or magpie object.

Usage

groupAggregate(data, vectorfunction=function(x){sum(x,na.rm=TRUE)},
dim=3, query=NULL, from=NULL, to=NULL, ...)

Arguments

data

A MAgPIE object or array

vectorfunction

Aggregation Type. Can be any vector function, and will be applied over the sub-vectors of "dim". E.g. the function is applied to the vector of temperate cereals in each region and each timestep.

dim

The dimension over which is aggregated. Can be 1,3 or higher. This dimension will be replaced by the larger categories specified in the query.

query

Query assigns the name dimension into categories. If Null, query is automatically searched for. If no query can be found, you have to manually select a query (csv or array), as well as specifiy "from" and "to"

from

Only needed if query is not null. From indicates the column within the query where the dimnames of data can be found.

to

Only needed if query is not null. To indicates the column within the query into which the dimnames shall be grouped.

...

additional arguments for the vectorfunction.

Value

Returns a magpie object. The name dimension "dim" is grouped according to the query, and the vectorfunction is applied on this groups to aggregate them to each on column.

Author(s)

Benjamin Bodirsky

See Also

colSums, superAggregate

Examples

## Not run: 
a <- new.magpie(cells_and_regions = c("ZAF.1","ZAR.2","VEN.3"),
                years = c("y1995","y2005"), 
                names = c("Wheat","Barley","Sugar_cane","Sugar_beet"), 
                fill=1:30)

#> a
#An object of class "magpie"
#, , Wheat
#
#      y1995 y2005
#ZAF.1     1     4
#ZAR.2     2     5
#VEN.3     3     6
#
#, , Barley
#
#      y1995 y2005
#ZAF.1     7    10
#ZAR.2     8    11
#VEN.3     9    12
#
#, , Sugar_cane
#
#      y1995 y2005
#ZAF.1    13    16
#ZAR.2    14    17
#VEN.3    15    18
#
#, , Sugar_beet
#
#      y1995 y2005
#ZAF.1    19    22
#ZAR.2    20    23
#VEN.3    21    24

b<-groupAggregate(a,dim=3)
#> b
#An object of class "magpie"
#, , tece
#
#      y1995 y2005
#ZAF.1     8    14
#ZAR.2    10    16
#VEN.3    12    18
#
#, , sugr_cane
#
#      y1995 y2005
#ZAF.1    13    16
#ZAR.2    14    17
#VEN.3    15    18
#
#, , sugr_beet
#
#      y1995 y2005
#ZAF.1    19    22
#ZAR.2    20    23
#VEN.3    21    24

c<-groupAggregate(a,dim=1)
#> c
#An object of class "magpie"
#, , Wheat
#
#      y1995 y2005
#AFR.1     3     9
#LAM.2     3     6
#
#, , Barley
#
#      y1995 y2005
#AFR.1    15    21
#LAM.2     9    12
#
#, , Sugar_cane
#
#      y1995 y2005
#AFR.1    27    33
#LAM.2    15    18
#
#, , Sugar_beet
#
#      y1995 y2005
#AFR.1    39    45
#LAM.2    21    24 

## End(Not run)

pik-piam/luscale documentation built on April 27, 2024, 2:11 p.m.