The Design Philosophy of Functions in sjmisc

Daniel Lüdecke

2018-02-03

Basically, this package covers two domains of functionality:

This vignette focusses on the second bullet point and gives an overview of data wrangling functions in this package.

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:

library(sjmisc)
data(efc)

# returns a vector
x <- rec(efc$e42dep, rec = "1,2=1; 3,4=2")
str(x)
#>  atomic [1:908] 2 2 2 2 2 2 2 2 2 2 ...
#>  - attr(*, "label")= chr "elder's dependency"

# returns a data frame (a tibble, to be exactly)
rec(efc, e42dep, rec = "1,2=1; 3,4=2", append = FALSE)
#> # A tibble: 908 x 1
#>    e42dep_r
#>       <dbl>
#>  1     2.00
#>  2     2.00
#>  3     2.00
#>  4     2.00
#>  5     2.00
#>  6     2.00
#>  7     2.00
#>  8     2.00
#>  9     2.00
#> 10     2.00
#> # ... with 898 more rows

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().

# select all variables with "cop" in their names, and also
# the range from c161sex to c175empl
rec(
  efc, contains("cop"), c161sex:c175empl, 
  rec = "0,1=0; else=1", 
  append = FALSE
)
#> # A tibble: 908 x 12
#>    c82cop1_r c83cop2_r c84cop3_r c85cop4_r c86cop5_r c87cop6_r c88cop7_r
#>        <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
#>  1      1.00      1.00      1.00      1.00      0         0         1.00
#>  2      1.00      1.00      1.00      1.00      1.00      0         1.00
#>  3      1.00      1.00      0         1.00      0         0         0   
#>  4      1.00      0         1.00      0         0         0         0   
#>  5      1.00      1.00      0         1.00      1.00      1.00      0   
#>  6      1.00      1.00      1.00      1.00      1.00      1.00      1.00
#>  7      1.00      1.00      1.00      0         0         1.00      1.00
#>  8      1.00      1.00      1.00      0         0         0         1.00
#>  9      1.00      1.00      1.00      1.00      1.00      0         1.00
#> 10      1.00      1.00      0         1.00      0         0         0   
#> # ... with 898 more rows, and 5 more variables: c89cop8_r <dbl>,
#> #   c90cop9_r <dbl>, c161sex_r <dbl>, c172code_r <dbl>, c175empl_r <dbl>

# center all variables with "age" in name, variable c12hour
# and all variables from column 19 to 21
center(efc, c12hour, contains("age"), 19:21, append = FALSE)
#> # A tibble: 908 x 6
#>    c12hour_c e17age_c c160age_c barthtot_c neg_c_7_c pos_v_4_c
#>        <dbl>    <dbl>     <dbl>      <dbl>     <dbl>     <dbl>
#>  1   - 26.4     3.88      2.54       10.5      0.150    -0.477
#>  2    106       8.88      0.537      10.5      8.15     -1.48 
#>  3     27.6     2.88     26.5       -29.5     -0.850     0.523
#>  4    126     -12.1      15.5       -64.5     -1.85      2.52 
#>  5    126       4.88    - 6.46      -39.5      0.150     2.52 
#>  6   - 26.4     5.88      2.54      - 4.55     7.15     -3.48 
#>  7    119     - 5.12      7.54      -59.5      3.15      0.523
#>  8     67.6     7.88     13.5       -29.5     -0.850     1.52 
#>  9   - 14.4   - 0.121     5.54      -49.5      3.15      0.523
#> 10   -  2.40    3.88    - 4.46      -64.5     -1.85      0.523
#> # ... with 898 more rows

The function-types

There are two types of function designs:

coercing/converting functions

Functions like to_factor() or to_label(), which convert variables into other types or add additional information like variable or value labels as attribute, typically return the complete data frame that was given as first argument without any new variables. The variables specified in the ...-ellipses argument are converted (overwritten), all other variables remain unchanged.

to_factor(efc, e42dep, e16sex)
#> # A tibble: 908 x 26
#>    c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4
#>      <dbl>    <dbl> <fct>   <dbl> <fct>    <dbl>   <dbl>   <dbl>   <dbl>
#>  1    16.0     2.00 2        83.0 3         3.00    2.00    2.00    2.00
#>  2   148       2.00 2        88.0 3         3.00    3.00    3.00    3.00
#>  3    70.0     1.00 2        82.0 3         2.00    2.00    1.00    4.00
#>  4   168       1.00 2        67.0 4         4.00    1.00    3.00    1.00
#>  5   168       2.00 2        84.0 4         3.00    2.00    1.00    2.00
#>  6    16.0     2.00 2        85.0 4         2.00    2.00    3.00    3.00
#>  7   161       1.00 1        74.0 4         4.00    2.00    4.00    1.00
#>  8   110       4.00 2        87.0 4         3.00    2.00    2.00    1.00
#>  9    28.0     2.00 2        79.0 4         3.00    2.00    3.00    2.00
#> 10    40.0     2.00 2        83.0 4         3.00    2.00    1.00    2.00
#> # ... with 898 more rows, and 17 more variables: c86cop5 <dbl>,
#> #   c87cop6 <dbl>, c88cop7 <dbl>, c89cop8 <dbl>, c90cop9 <dbl>,
#> #   c160age <dbl>, c161sex <dbl>, c172code <dbl>, c175empl <dbl>,
#> #   barthtot <dbl>, neg_c_7 <dbl>, pos_v_4 <dbl>, quol_5 <dbl>,
#> #   resttotn <dbl>, tot_sc_e <dbl>, n4pstu <dbl>, nur_pst <dbl>

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.

# complete data, including new columns
rec(efc, c82cop1, c83cop2, rec = "1,2=0; 3:4=2", append = TRUE)
#> # A tibble: 908 x 28
#>    c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4
#>      <dbl>    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1    16.0     2.00   2.00   83.0   3.00    3.00    2.00    2.00    2.00
#>  2   148       2.00   2.00   88.0   3.00    3.00    3.00    3.00    3.00
#>  3    70.0     1.00   2.00   82.0   3.00    2.00    2.00    1.00    4.00
#>  4   168       1.00   2.00   67.0   4.00    4.00    1.00    3.00    1.00
#>  5   168       2.00   2.00   84.0   4.00    3.00    2.00    1.00    2.00
#>  6    16.0     2.00   2.00   85.0   4.00    2.00    2.00    3.00    3.00
#>  7   161       1.00   1.00   74.0   4.00    4.00    2.00    4.00    1.00
#>  8   110       4.00   2.00   87.0   4.00    3.00    2.00    2.00    1.00
#>  9    28.0     2.00   2.00   79.0   4.00    3.00    2.00    3.00    2.00
#> 10    40.0     2.00   2.00   83.0   4.00    3.00    2.00    1.00    2.00
#> # ... with 898 more rows, and 19 more variables: c86cop5 <dbl>,
#> #   c87cop6 <dbl>, c88cop7 <dbl>, c89cop8 <dbl>, c90cop9 <dbl>,
#> #   c160age <dbl>, c161sex <dbl>, c172code <dbl>, c175empl <dbl>,
#> #   barthtot <dbl>, neg_c_7 <dbl>, pos_v_4 <dbl>, quol_5 <dbl>,
#> #   resttotn <dbl>, tot_sc_e <dbl>, n4pstu <dbl>, nur_pst <dbl>,
#> #   c82cop1_r <dbl>, c83cop2_r <dbl>

# only new columns
rec(efc, c82cop1, c83cop2, rec = "1,2=0; 3:4=2", append = FALSE)
#> # A tibble: 908 x 2
#>    c82cop1_r c83cop2_r
#>        <dbl>     <dbl>
#>  1      2.00      0   
#>  2      2.00      2.00
#>  3      0         0   
#>  4      2.00      0   
#>  5      2.00      0   
#>  6      0         0   
#>  7      2.00      0   
#>  8      2.00      0   
#>  9      2.00      0   
#> 10      2.00      0   
#> # ... with 898 more rows

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.

efc %>% 
  rec(c82cop1, c83cop2, rec = "1,2=0; 3:4=2", append = FALSE) %>% 
  add_columns(efc)
#> # A tibble: 908 x 28
#>    c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4
#>      <dbl>    <dbl>  <dbl>  <dbl>  <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1    16.0     2.00   2.00   83.0   3.00    3.00    2.00    2.00    2.00
#>  2   148       2.00   2.00   88.0   3.00    3.00    3.00    3.00    3.00
#>  3    70.0     1.00   2.00   82.0   3.00    2.00    2.00    1.00    4.00
#>  4   168       1.00   2.00   67.0   4.00    4.00    1.00    3.00    1.00
#>  5   168       2.00   2.00   84.0   4.00    3.00    2.00    1.00    2.00
#>  6    16.0     2.00   2.00   85.0   4.00    2.00    2.00    3.00    3.00
#>  7   161       1.00   1.00   74.0   4.00    4.00    2.00    4.00    1.00
#>  8   110       4.00   2.00   87.0   4.00    3.00    2.00    2.00    1.00
#>  9    28.0     2.00   2.00   79.0   4.00    3.00    2.00    3.00    2.00
#> 10    40.0     2.00   2.00   83.0   4.00    3.00    2.00    1.00    2.00
#> # ... with 898 more rows, and 19 more variables: c86cop5 <dbl>,
#> #   c87cop6 <dbl>, c88cop7 <dbl>, c89cop8 <dbl>, c90cop9 <dbl>,
#> #   c160age <dbl>, c161sex <dbl>, c172code <dbl>, c175empl <dbl>,
#> #   barthtot <dbl>, neg_c_7 <dbl>, pos_v_4 <dbl>, quol_5 <dbl>,
#> #   resttotn <dbl>, tot_sc_e <dbl>, n4pstu <dbl>, nur_pst <dbl>,
#> #   c82cop1_r <dbl>, c83cop2_r <dbl>

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():

efc %>% 
  select(c82cop1, c83cop2) %>% 
  rec(rec = "1,2=0; 3:4=2")
#> # A tibble: 908 x 4
#>    c82cop1 c83cop2 c82cop1_r c83cop2_r
#>      <dbl>   <dbl>     <dbl>     <dbl>
#>  1    3.00    2.00      2.00      0   
#>  2    3.00    3.00      2.00      2.00
#>  3    2.00    2.00      0         0   
#>  4    4.00    1.00      2.00      0   
#>  5    3.00    2.00      2.00      0   
#>  6    2.00    2.00      0         0   
#>  7    4.00    2.00      2.00      0   
#>  8    3.00    2.00      2.00      0   
#>  9    3.00    2.00      2.00      0   
#> 10    3.00    2.00      2.00      0   
#> # ... with 898 more rows

efc %>% 
  select(c82cop1, c83cop2) %>% 
  mutate(
    c82cop1_dicho = rec(c82cop1, rec = "1,2=0; 3:4=2"),
    c83cop2_dicho = rec(c83cop2, rec = "1,2=0; 3:4=2")
  ) %>% 
  head()
#>   c82cop1 c83cop2 c82cop1_dicho c83cop2_dicho
#> 1       3       2             2             0
#> 2       3       3             2             2
#> 3       2       2             0             0
#> 4       4       1             2             0
#> 5       3       2             2             0
#> 6       2       2             0             0