Learn R Programming

eatGADS (version 1.1.1)

composeVar: Create a composite variable.

Description

Create a composite variable out of two variables.

Usage

composeVar(GADSdat, sourceVars, primarySourceVar, newVar, checkVarName = TRUE)

Value

The modified GADSdat.

Arguments

GADSdat

GADSdat or all_GADSdat object imported via eatGADS.

sourceVars

Character vector of length two containing the variable names which represent the sources of information.

primarySourceVar

Character vector containing a single variable name. Which of the sourceVars should be preferred?

newVar

Character vector containing the name of the new composite variable.

checkVarName

Logical. Should newVar be checked by checkVarNames?

Details

A common use case for creating a composite variable is if there are multiple sources for the same information. For example, a child and the parents are asked about the child's native language. In such cases a composite variable contains information from both variables, meaning that one source is preferred and the other source is used to substitute missing values.

Examples

Run this code
# example data
dat <- data.frame(ID = 1:4,
nat_lang_child = c("Engl", "Ger", "missing", "missing"),
nat_lang_father = c("Engl", "Engl", "Ger", "missing"),
stringsAsFactors = TRUE)
gads <- import_DF(dat)
changeMissings(gads, "nat_lang_child", value = 3, missings = "miss")
changeMissings(gads, "nat_lang_father", value = 3, missings = "miss")

# compose variable
composeVar(gads, sourceVars = c("nat_lang_child", "nat_lang_father"),
           primarySourceVar = "nat_lang_child", newVar = "nat_lang_comp")


Run the code above in your browser using DataLab