mixed_labelled: Mixed labelled vectors

Description Usage Arguments Details Value Examples

Description

Use mixed labelled vectors to analyse social science data

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
mixed_labelled(
    x = double(),
    labels = NULL,
    na_values = NULL,
    na_range = NULL,
    label = NULL,
    ...
)

is_mixed(x)

as_mixed(x, ...)

unmix(x, haven = FALSE)

Arguments

x

A numeric vector to label, or a mixed labelled vector (for unmix)

labels

A named vector or NULL. The vector should be the same type as x. Unlike factors, labels don't need to be exhaustive: only a fraction of the values might be labelled.

na_values

A vector of values that should also be considered as missing.

na_range

A numeric vector of length two giving the (inclusive) extents of the range. Use -Inf and Inf if you want the range to be open ended.

label

A short, human-readable description of the vector.

haven

Logical, coerce the unmixed object to class "haven_labelled".

...

Other arguments used by various other methods.

Details

The mixed_labelled objects are very similar to the haven_labelled_spss objects from package haven. It has exactly the same arguments, but it features a fundamental difference in the treatment of (declared) missing values.

In package haven, existing values are treated as if they were missing. By contrast, in package mixed the NA values are treated as existing values.

This difference is fundamental and points to an inconsistency in package haven: while existing values can be identified as missing using the function is.na(), they are in fact present in the vector and other packages (most importantly the core ones) will not know these values should be treated as missing.

Consequently, the existing values are interpreted as missing only by package haven. Statistical procedures will use those values as just like they were valid values.

Package mixed approaches the problem in exactly the opposite way: instead of treating existing values as missing, it treats (certain) NA values as existing. This way, it does not run into the risk of using values which should not be used.

The function unmix() replaces the NA entries into their original numeric values, and drops all attributes related to missing values: na_values, na_range and na_index.

Value

mixed_labelled() and as_mixed() will return a labelled vector.

is_mixed() will return a logical scalar.

unmix() will return a regular vector if argument haven is FALSE, or a haven type of labelled vector when set to TRUE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
x <- mixed_labelled(c(1:5, -1),
                    labels = c(Good = 1, Bad = 5, DK = -1),
                    na_values = -1)
x

is.na(x)

x > 0

x == -1


# when newly added values are already declared as missing, they are automatically coerced
c(x, 2, -1)

# switch NAs with their original values
unmix(x)

dusadrian/mixed documentation built on Dec. 20, 2021, 2:16 a.m.