coicop: COICOP codes, bundles and relatives

coicopR Documentation

COICOP codes, bundles and relatives

Description

Function is.coicop() checks if the input is a valid COICOP code while level() returns the level (e.g. division or subclass).

For HICP data, COICOP codes are sometimes merged into bundles (e.g. 08X, 0531_2), deviating from the usual code structure. Function is.bundle() flags if a COICOP code is a bundle or not, while unbundle() resolves the bundles into the underlying valid codes.

Functions parent() and child() derive the higher-level parents or lower-level children of a COICOP code.

Usage

is.coicop(id, settings=list())

level(id, label=FALSE, settings=list())

is.bundle(id, settings=list())

unbundle(id, settings=list())

child(id, usedict=TRUE, closest=TRUE, k=1, settings=list())

parent(id, usedict=TRUE, closest=TRUE, k=1, settings=list())

Arguments

id

character vector of COICOP codes.

label

logical indicating if the number of digits or the labels (e.g., division, subclass) should be returned.

usedict

logical indicating if parents or children should be derived from the full code dictionary defined by settings$coicop.version (if set to TRUE) or only from the codes present in id.

closest

logical indicating if the closest parents or children should be derived or the k-th ones defined by k. For example, if set to TRUE, the closest parent could be the direct parent for one code (e.g. 031->03) and the grandparent for another (e.g. 0321->03).

k

integer specifying the k-th relative (e.g., 1 for direct parents or children, 2 for grandparents and grandchildren, ...). Multiple values allowed, e.g., k=c(1,2). Only relevant if closest=FALSE.

settings

list of control settings to be used. The following settings are supported:

  • coicop.version : character specifying the COICOP version to be used by is.coicop(), level(), child(), and parent() for flagging valid COICOP codes. See details for the allowed values. The default is getOption("hicp.coicop.version").

  • all.items.code : character specifying the code internally used by level(), child(), and parent() for the all-items index. The default is taken from getOption("hicp.all.items.code").

  • coicop.bundles : named list specifying the COICOP bundle code dictionary used for unbundling any bundle codes in id. The default is getOption("hicp.coicop.bundles").

  • simplify : logical indicating if the output of child(), parent() or unbundle() should be simplified into a vector if possible. The default is FALSE.

    • For child() and parent(): If both a COICOP bundle code and the underlying codes are the parent or child, only the latter codes are kept (e.g., c(08X,082)->082). Note that simplification usually only works for parent().

    • For unbundle(): All codes underlying the bundle code are kept, meaning that the resulting vector length can exceed the length of id.

Details

The following COICOP versions are supported:

  • Classification of Individual Consumption According to Purpose (COICOP-1999): coicop1999

  • European COICOP (version 1, ECOICOP): ecoicop

  • ECOICOP adopted to the needs of the HICP (version 1, ECOICOP-HICP): ecoicop.hicp

  • COICOP-2018: coicop2018

  • ECOICOP (version 2, ECOICOP 2): ecoicop2

The COICOP version can be set temporarily in the function settings or globally via options(hicp.coicop.version). The package default is ecoicop.hicp.

None of the COICOP versions include a code for the all-items index. By default, the internal package code for the all-items index is defined by options(hicp.all.items.code="00") but can be changed by the user. The level is always 1.

Although bundle codes (e.g. 08X, 0531_2) are no valid COICOP codes, they are internally resolved into their underlying codes and processed in that way if they can be found in the bundle code dictionary (see getOption("hicp.coicop.bundles")). If bundle codes should not be processed, the dictionary can be cleared by options("hicp.coicop.bundles"=list()).

Value

Functions is.coicop() and is.bundle() return a logical vector, function level() an integer vector, and functions child(), parent(), and unbundle() a list. All function outputs have the same length as id.

Author(s)

Sebastian Weinand

See Also

tree

Examples

### EXAMPLE 1

# check if coicop codes are valid:
is.coicop(id=c("00","CP00","01","011","13","08X"))

# get the coicop level or label:
level(id=c("00","05","053","0531_2"))
level(id=c("00","05","053","0531_2"), label=TRUE)

# derive children and parents

# no children of 01 present in ids:
child(id=c("01"), usedict=FALSE) 

# still no direct child present:
child(id=c("01","0111"), usedict=FALSE, closest=FALSE, k=1) 

# but a grandchild of 01 is found:
child(id=c("01","0111"), usedict=FALSE, closest=TRUE) 

# derive the children from the code dictionary:
child(id=c("01"), usedict=TRUE) 

# two parents found for 05311 due to presence of bundle code:
parent(id=c("0531","0531_2","05311","05321"), usedict=FALSE) 

# simplification removes bundle code:
parent(id=c("0531","0531_2","05311","05321"), usedict=FALSE, settings=list(simplify=TRUE))

### EXAMPLE 2: Working with published HICP data

library(data.table)
library(restatapi)
options(restatapi_cores=1) # set cores for testing on CRAN
options(hicp.chatty=FALSE) # suppress package messages and warnings

# load hicp item weights of euro area:
coicops <- hicp::data(id="prc_hicp_inw", filter=list(geo="EA"))
coicops <- coicops[grepl("^CP", coicop),]
coicops[, "coicop":=gsub("^CP", "", coicop)]

# show frequency of coicop levels over time:
coicops[, .N, by=list(time, "lvl"=level(coicop))]

# get coicop parent from the data:
coicops[, "parent":=parent(id=coicop, usedict=FALSE, settings=list(simplify=TRUE)), by="time"]

# flag if coicop has child available in the data:
coicops[, "has_child":=lengths(child(id=coicop, usedict=FALSE))>0, by="time"]
coicops[has_child==FALSE, sum(values, na.rm=TRUE), by="time"]
# coicop bundles and their component ids are both taken into
# account. this double counting explains some differences

hicp documentation built on Aug. 8, 2025, 6:30 p.m.