pik-piam / magclass

R package | Data Class and Tools for Handling Spatial-Temporal Data
GNU Lesser General Public License v3.0
4 stars 24 forks source link

`[` method with pmatch = TRUE may return items multiple times #115

Closed 0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q closed 2 years ago

0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q commented 2 years ago

Minimal example

library(mrdrivers)

setConfig(forcecache = TRUE)

GDP <- calcOutput('GDP')

str(GDP[,,c('SSP2', 'SSP2EU'), pmatch = TRUE])

returns the gdp_SSP2EU variable twice, as it is matched by both SSP2 and SSP2EU.

Formal class 'magpie' [package "magclass"] with 1 slot
  ..@ .Data: num [1:12, 1:38, 1:3] 1436063 722606 565864 4964586 419590 ...
  .. ..- attr(*, "dimnames")=List of 3
  .. .. ..$ iso3c   : chr [1:12] "LAM" "OAS" "SSA" "EUR" ...
  .. .. ..$ year    : chr [1:38] "y1965" "y1970" "y1975" "y1980" ...
  .. .. ..$ variable: chr [1:3] "gdp_SSP2" "gdp_SSP2EU" "gdp_SSP2EU"

I suspect the behaviour is not intended and it limits the usefulness of the pmatch option.

tscheypidi commented 2 years ago

This behavior is in fact intended and is basically inherited from the default behavior under pmatch = FALSE. Here an example:

> p <- maxample("pop")[,1,1]
> p[c("AFR","AFR"),,]
An object of class "magpie"
, , scenario = A2

     t
i        y1995
  AFR 552.6664
  AFR 552.6664

Running the same thing with pmatch = TRUE it would be inconsistent to only have a single entry returned with the otherwise identical call:

> p[c("AFR","AFR"),,,pmatch = TRUE]
An object of class "magpie"
, , scenario = A2

     t
i        y1995
  AFR 552.6664
  AFR 552.6664