crunch (version 1.27.7)

slideCategories: Create sliding subvariable definitions

Description

Create a multiple response array variable by sliding through category levels and selecting potentially overlapping sets of categories.

Usage

slideCategories(variable, step, width, ..., complete = TRUE, useNA = FALSE)

Arguments

variable

A categorical crunch variable

step

number of categories between starting points of groups

width

number of categories wide the grouping should be

...

additional attributes to be included in the VariableDefinition, can be either functions that take the category names to be included in the sliding group and returns a single string, or a character vector the same length as the number of subvariables that will be created.

complete

whether to only include category groupings that are as wide as width (defaults to TRUE)

useNA

whether to use missing categories from the original variable (defaults to FALSE)

Value

A list of VariableDefinitions appropriate for use in deriveArray()

Examples

Run this code
# NOT RUN {
login()
data <- data.frame(
    wave = factor(c("a", "b", "c", "d", "e"))
)

ds <- newDataset(data, "Sliding Categories")

# Make an MR variable where subvariable is 1 step apart, and with 3 categories wide
# and name subvariables with vector
ds$wave_step1_wide3 <- deriveArray(
   slideCategories(ds$wave, step = 1, width = 3, name = c("a - c", "b - d", "c - e")),
   "Sliding example 1"
)

# You can also make names (and other subvariable metadata like alias or description)
# with a function:
ds$wave_step2_wide2 <- deriveArray(
   slideCategories(
     ds$wave,
     step = 2,
     width = 2,
     name = function(x) paste(x[1], "-", x[length(x)])
   ),
   "Sliding example 2"
)
# }

Run the code above in your browser using DataCamp Workspace