Creates dummy variables from multiple categorical variables. Uses data.table() for speed (enhanced from the previous version) Uses dplyr select() special functions to select categorical variables.

superspread(df, select_helpers)

Arguments

df

Data frame containing the multiple categorical variables.

select_helpers

Uses dplyr-style select functions to select multiple variables. Use everything() to select all variables. These variables must all be character type.

See also

Other superspread functions: superspread_count()

Examples

superspread(iris,"Species")
#> # A tibble: 150 × 8
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species setosa versicolor
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <lgl>  <lgl>     
#>  1          5.1         3.5          1.4         0.2 setosa  TRUE   FALSE     
#>  2          4.9         3            1.4         0.2 setosa  TRUE   FALSE     
#>  3          4.7         3.2          1.3         0.2 setosa  TRUE   FALSE     
#>  4          4.6         3.1          1.5         0.2 setosa  TRUE   FALSE     
#>  5          5           3.6          1.4         0.2 setosa  TRUE   FALSE     
#>  6          5.4         3.9          1.7         0.4 setosa  TRUE   FALSE     
#>  7          4.6         3.4          1.4         0.3 setosa  TRUE   FALSE     
#>  8          5           3.4          1.5         0.2 setosa  TRUE   FALSE     
#>  9          4.4         2.9          1.4         0.2 setosa  TRUE   FALSE     
#> 10          4.9         3.1          1.5         0.1 setosa  TRUE   FALSE     
#> # ℹ 140 more rows
#> # ℹ 1 more variable: virginica <lgl>
if (FALSE) {
library(data.table)
library(dplyr)
dt <- data.table(id = 1:10000,
                 cat1 = sample(letters[1:3],10000,replace = TRUE),
                 cat2 = sample(letters[1:4],10000,replace = TRUE),
                 cat3 = sample(letters[1:5],10000,replace = TRUE))
dt <- as.data.table(dt)
superspread(df = dt, select_helpers = contains("cat"))
}