dem.add: Construct new Variables

Description Usage Arguments Details Value Examples

View source: R/vars.r

Description

dem.add is a generic function. It is intended to by applied to the dem object and constructs new variables based on existing ones. The new variables are added as columns to dem and the labels to the variable values are added to id$lab.

Usage

1
dem.add(x, what, id. = id, breaks = c(3, 15, 20, 30, 40, 50, 60))

Arguments

x

A data.tabel, typically dem.

what

A length one character vector. What set of variabels? Corrently available are: 'rim','calendar','ageclass','gm','zone','lifestage', see details.

id.

The id object. See id <- setup().

breaks

An optional numeric vector, sepcifying the age breaks to split age into ageclasses. Default are the 7-folded ageclasses used to weight the TV-panel: c(3,15,20,30,40,50,60).

Details

To allow the construction of new variabels based on other variabels, these other variabels have to be present in the x (usually dem). This means they have to be imported in the first place, e.g.: setup(..., dem.var = c('v1','v2')). How do we know which variables are needed? There is a parameter need in each subfunction where the needed variabels are listed. So, to add rims first ask what variabels are needed and the import the data accordingly, see examples.

Careful with 'gm' and 'zone'. These read the corresponding CSV file in ../data/meta/.

Value

x, the input data.table (usually dem) is changed in place. The new variabels are added as new columns to x and the corresponding variable values and labels are added to id$lab.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
library(tv)
dem.var <- eval(formals(tv:::dem.add.rim)$need)
setup('2018-01-01', 3, dem.var = dem.var, import = TRUE, view = FALSE, prg = FALSE)
dem[]               # dem contains additional variables specified in dem.var
dem.add(dem, 'rim') # construct new variables 'rim' (weighting rims).
dem[]               # The new variables are added to dem
id$lab$rim_agesex   # And theis labels are added to id$lab

# dem.add.calendar only needs 'day' which is always present:
setup('2018-01-01', 3, import = TRUE, view = FALSE, prg = FALSE, dem.uni = FALSE)
dem.add(dem, 'calendar')
id$lab$month

rluech/tv-clone documentation built on Jan. 7, 2022, 12:27 a.m.