Skip to contents

Modified mtcars dataset so:

  • every column is labelled (using label attribute)

  • rownames are a character column named model

  • gear and cyl columns are considered as numerical factors

  • vs and am columns are considered as character vector

See mtcars for more informations on the original "Motor Trend Car Road Tests" dataset.

Usage

mtcars2

Format

A data frame with 32 observations on 11 variables with labels.

Source


library(dplyr)
mtcars2 = mtcars %>%
    mutate(
       model=rownames(mtcars),
       vs=ifelse(vs==0, "vshaped", "straight"),
       am=ifelse(am==0, "auto", "manual"),
       across(c("cyl", "gear"), factor),
       .before=1
    ) %>%
    expss::apply_labels( #I also could have used [import_labels] or even `labelled::set_variable_labels()`
        mpg="Miles/(US) gallon",
        cyl="Number of cylinders",
        disp="Displacement (cu.in.)",
        hp="Gross horsepower",
        drat="Rear axle ratio",
        wt="Weight (1000 lbs)",
        qsec="1/4 mile time",
        vs="Engine",
        am="Transmission",
        gear="Number of forward gears",
       carb="Number of carburetors"
    )

Examples

library(crosstable)
ct=crosstable(mtcars2, by=vs)
ct
#> # A tibble: 76 × 5
#>    .id   label variable           straight    vshaped    
#>    <chr> <chr> <chr>              <chr>       <chr>      
#>  1 model Model AMC Javelin        0 (0%)      1 (100.00%)
#>  2 model Model Cadillac Fleetwood 0 (0%)      1 (100.00%)
#>  3 model Model Camaro Z28         0 (0%)      1 (100.00%)
#>  4 model Model Chrysler Imperial  0 (0%)      1 (100.00%)
#>  5 model Model Datsun 710         1 (100.00%) 0 (0%)     
#>  6 model Model Dodge Challenger   0 (0%)      1 (100.00%)
#>  7 model Model Duster 360         0 (0%)      1 (100.00%)
#>  8 model Model Ferrari Dino       0 (0%)      1 (100.00%)
#>  9 model Model Fiat 128           1 (100.00%) 0 (0%)     
#> 10 model Model Fiat X1-9          1 (100.00%) 0 (0%)     
#> # … with 66 more rows
as_flextable(ct)
#> a flextable object.
#> col_keys: `label`, `variable`, `straight`, `vshaped` 
#> header has 2 row(s) 
#> body has 76 row(s) 
#> original dataset sample: 
#>     .id label           variable    straight     vshaped
#> 1 model Model        AMC Javelin      0 (0%) 1 (100.00%)
#> 2 model Model Cadillac Fleetwood      0 (0%) 1 (100.00%)
#> 3 model Model         Camaro Z28      0 (0%) 1 (100.00%)
#> 4 model Model  Chrysler Imperial      0 (0%) 1 (100.00%)
#> 5 model Model         Datsun 710 1 (100.00%)      0 (0%)