haver.setalias {Haver} | R Documentation

## Assign Aliases to Haver Series Codes

### Description

`haver.setalias` enables you to define your own series codes and use them in data analysis. You can assign a user-defined series code (an alias) to any Haver series in a `HaverMetaData` or `HaverData` object. The original Haver code also remains recorded.

### Usage

    haver.setalias(x, codes, aliases, clear=FALSE)

### Arguments

`x` | a `HaverMetaData` or a `HaverData` object.
`codes` | a character vector specifying Haver series codes that are to be given user-defined aliases (supplied by `aliases`). Note that `codes` must not contain database names.
`aliases` | a character vector. Specifies the aliases that are to be given to the Haver series codes in `codes`. Must be of the same length as `codes`.
`clear` | a logical `TRUE` / `FALSE`. If `clear=TRUE`, `haver.setalias` removes all information regarding aliases.

### Value

The return value is of the same type as the first argument to the function, i.e. either `HaverMetaData` or `HaverData`.

### Assigning Aliases

User-defined codes are stored in field ‘alias’. Since the original Haver codes remain in field ‘code’, any mapping from an original Haver code to a user-defined alias is recorded in the object.

In order to define your own codes, you have to supply a mapping of original Haver series codes to the codes that you want to use. You do so through the method `haver.setalias` and its arguments `codes` (the original Haver series codes) and `aliases` (the codes that you want to use). The two character vectors must be of the same length. Elements of argument `aliases` must not exist in field `code`. For example, if this field contains a code ‘yr’, you cannot use this string as an alias for any of the codes in the object.

`codes` may contain codes that are not in the `HaverMetaData` or `HaverData` object, and vice versa. Among other things, this means that you can assign your own codes to any subset of the codes of the object. A requirement that always applies is that the mapping you supply must be 1:1, i.e. you may not assign an original Haver series code to two different user-defined codes, and you may not assign the same user-defined code to two different Haver codes.

You may set aliases of series codes incrementally. For example, you can issue a statement that assigns aliases for the first three codes, then issue a statement that assigns aliases for some other codes, etc. Each renaming operation requires, however, that the mapping supplied in `codes` and `aliases` is consistent with the mapping that already exists in the object. As an immediate consequence, you cannot assign an alias to a series code that already has a different alias assigned. If you want to do this, you have to clear all user-defined codes first using the `clear=TRUE` argument. You may wish to save the mapping that exists in the object before you do that. You can do this easily by accessing the ‘code’ and ‘alias’ fields of the object using double bracket indexing.

Haver series codes in `HaverMetaData` objects are always recorded in lower case. Any codes that you specify in `aliases` will also be converted to lower case. This may result in mappings that are not allowed. For example, the mapping from `codes=c("code1","code2")` to
`aliases=c("mycode","myCode")` will not work since it is not a 1:1 mapping anymore once the elements in `aliases` are translated to lower case.

`haver.setalias` does not modify the `HaverMetaData` object in place but rather returns a modified object. Only statements that have some sort of an assignment like
`> hmd <- haver.setalias(hmd, codes, aliases)`
make sense.

Note: If you have used `haver.mdfields` to set `mdfield_alias=FALSE`, `haver.setalias` will refuse to operate on objects returned by queries run under that setting.

### See Also

See Haver_package for all help entries related to the Haver package.

### Examples

    haver.path()
    haver.path("examples")

    ## We set up an example data set consisting of three series:
    hd <- haver.data(c("haverd:fxtwb", "havermqa:gdph", "havermqa:c")
                     , aggmode="relaxed", start="2010-10-31")
    haver.datamd(hd)

    ## We assign aliases for the first two codes:
    ex_codes   <- c("fxtwb", "gdph")
    ex_aliases <- c("fx"   , "y"   )
    hd  <- haver.setalias(hd, codes=ex_codes, aliases=ex_aliases)
    hmd <- haver.datamd(hd)
    print(hmd, fields=c("code", "alias", "descriptor"))

    ## We add an alias for the third code:
    hd  <- haver.setalias(hd, codes="c", aliases="pce")

    ## The aliases are used when coercing the object to another type:
    as.matrix(hd)

    haver.path("restore")

[Package Haver version 3.2 Index]