knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(categorical)
library(tibble)

Categorical Vectors

features

This package introduces a new Categorical vector type. With the following features:

  1. A fixed set of values called 'levels' (just like factors)
  2. each record in the vector can have more than one level 'selected' to allow storing multipe response survey data.
  3. Alternative values can be added for each level; you can switch back and forth between alternatives. Alternatives can be of any vector type.
  4. A second set of "internal" alternative values can be set, to make it easy to create new vector types from the categorical type.

Levels

Alternative Levels

let's store the days of the week as integer values 1-7, but include labels in two different languages, and another alternative value that defines whether the day is on the weekend or not:

days <- categorical(c(1,2,1,5,6,3,7),
                                levels=1:7,
                                alternatives = list(
                                  labels_english = c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
                                  labels_german = c('Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'),
                                  is_weekend = c(FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE)
                                )
                                )

we can alternate between the original and alternative values:

alternate(days, "labels_english")
alternate(days, "is_weekend")

let's subset the vector to only keep the week days, then get their English labels:

alternate(days, "labels_english")[alternate(days, "is_weekend")]

Multiple selection

categorical vectors support multiple selection. That means that for each element of a vector, each level can be 'selected' or not selected:

# What are your favourite colours? (some people like more than one color, some even no colour at all!)

fav_colours <- categorical(list(
  c('red'),
  c('blue', 'orange', 'yellow'),
  c('yellow', 'magenta'),
  c('black'),
  c()
),
levels = c('red', 'blue', 'orange', 'yellow', 'magenta', 'black'))

fav_colours

We see that the second item has three colors selected, the last item none at all.

Since we didn't specify the levels, they were set automatically to the unique values in the supplied list. Note that the levels correspond to all possible answers, not answer combinations:

levels(fav_colours)

Ordinal class

# colors ordered by wavelength
my_lightwaves<-ordinal(x = c('red','infra','violet','blue','red', 'infra'),
                levels = c('violet','blue','red', 'infra'),
                alternatives = list(rank = c(1,2,3,4)))

my_lightwaves

Ordinals as all categorical classes support multiple selection:

my_lightwaves<-ordinal(x = list(c('red','infra'),
                                c(),
                                c('blue','red'),
                                'infra'),
                levels = c('violet','blue','red', 'infra'),
                alternatives = list(rank = c(1,2,3,4)))



my_lightwaves


mabafaba/categorical documentation built on May 3, 2020, 9 p.m.