copy_lev: Copy Survey Data Variable Levels and Question Text

Description Usage Arguments Details Value Examples

Description

Applies levels and question text attributes to variables in a subsetted svy_df object using the levels from the columns with matching names in the original data frame.

Usage

1
copy_lev(x, y)

Arguments

x

the new data frame that is a subset of another data frame object.

y

the original data frame containing the variable levels.

Details

When subsetting a data frame or similar R object, variable levels (for factors and non-factors) and other data attributes are not copied using any common function. Rather than applying the levels for each variable by hand, this function completes the process in one call by matching the names of variables in the subset data fram with those in the original, and then copying the matched variable levels in the original object to the new one.

Value

A data frame where all matched column names have variable levels.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## subset a data frame
x <- data[, c('nr1', 'nr2Bin', 'nr2', 'nr3')]
levels(x$nr1)
#> NULL

## copy the levels
x <- copy_lev(x, data)
levels(x$nr1)

## Do it with dplyr/chaining
library(dplyr)
x <- data[, c('nr1', 'nr2Bin', 'nr2', 'nr3')] %>% copy_lev(data)
levels(x$nr1)

alexdulin/MCmrp documentation built on May 11, 2019, 11:29 p.m.