The Design Philosophy of Functions in sjmisc

Daniel Lüdecke


Basically, this package complements the dplyr package in that sjmisc takes over data transformation tasks on variables, like recoding, dichotomizing or grouping variables, setting and replacing missing values, etc. The data transformation functions also support labelled data.

The design of data transformation functions

The design of data transformation functions in this package follows, where appropriate, the tidyverse-approach, with the first argument of a function always being the data (either a data frame or vector), followed by variable names that should be processed by the function. If no variables are specified as argument, the function applies to the complete data that was indicated as first function argument.

The data-argument

A major difference to dplyr-functions like select() or filter() is that the data-argument (the first argument of each function), may either be a data frame or a vector. The returned object for each function equals the type of the data-argument:

This design-choice is mainly due to compatibility- and convenience-reasons. It does not affect the usual “tidyverse-workflow” or when using pipe-chains.

The …-ellipses-argument

The selection of variables specified in the ...-ellipses-argument is powered by dplyr’s select() and tidyselect’s select_helpers(). This means, you can use existing functions like : to select a range of variables, or also use tidyselect’s select_helpers, like contains() or one_of().

The function-types

There are two types of function designs:

transformation/recoding functions

Functions like rec() or dicho(), which transform or recode variables, by default add the transformed or recoded variables to the data frame, so they return the new variables and the original data as combined data frame. To return only the transformed and recoded variables specified in the ...-ellipses argument, use argument append = FALSE.

These variables usually get a suffix, so you can bind these variables as new columns to a data frame, for instance with add_columns(). The function add_columns() is useful if you want to bind/add columns within a pipe-chain to the end of a data frame.

sjmisc and dplyr

The functions of sjmisc are designed to work together seamlessly with other packes from the tidyverse, like dplyr. For instance, you can use the functions from sjmisc both within a pipe-worklflow to manipulate data frames, or to create new variables with mutate():