knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(categorical) library(tibble)
This package introduces a new Categorical vector type. With the following features:
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")]
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)
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.