Skip to contents

Set the "label" attribute of an object

Copy the label from one variable to another

Usage

set_label(x, value, object = FALSE)

copy_label_from(x, from)

Arguments

x

the variable to label

value

value of the label. If x is a list/data.frame, all the labels will be set recursively

object

if x is a list/data.frame, object=TRUE will force setting the labels of the object instead of the children

from

the variable whose label must be copied

Value

An object of the same type as x, with labels

An object of the same type as x, with the label of from

Author

Dan Chaltiel

Examples

library(dplyr)
mtcars %>%
   mutate(mpg2=set_label(mpg, "Miles per gallon"),
          mpg3=mpg %>% copy_label_from(mpg2)) %>%
   crosstable(c(mpg, mpg2, mpg3))
#> # A tibble: 12 × 4
#>    .id   label            variable   value           
#>    <chr> <chr>            <chr>      <chr>           
#>  1 mpg   mpg              Min / Max  10.4 / 33.9     
#>  2 mpg   mpg              Med [IQR]  19.2 [15.4;22.8]
#>  3 mpg   mpg              Mean (std) 20.1 (6.0)      
#>  4 mpg   mpg              N (NA)     32 (0)          
#>  5 mpg2  Miles per gallon Min / Max  10.4 / 33.9     
#>  6 mpg2  Miles per gallon Med [IQR]  19.2 [15.4;22.8]
#>  7 mpg2  Miles per gallon Mean (std) 20.1 (6.0)      
#>  8 mpg2  Miles per gallon N (NA)     32 (0)          
#>  9 mpg3  Miles per gallon Min / Max  10.4 / 33.9     
#> 10 mpg3  Miles per gallon Med [IQR]  19.2 [15.4;22.8]
#> 11 mpg3  Miles per gallon Mean (std) 20.1 (6.0)      
#> 12 mpg3  Miles per gallon N (NA)     32 (0)          
mtcars %>%
   copy_label_from(mtcars2[,1:11]) %>%
   crosstable(c(mpg, vs))
#> # A tibble: 6 × 4
#>   .id   label         variable   value           
#>   <chr> <chr>         <chr>      <chr>           
#> 1 mpg   Model         Min / Max  10.4 / 33.9     
#> 2 mpg   Model         Med [IQR]  19.2 [15.4;22.8]
#> 3 mpg   Model         Mean (std) 20.1 (6.0)      
#> 4 mpg   Model         N (NA)     32 (0)          
#> 5 vs    1/4 mile time 0          18 (56.25%)     
#> 6 vs    1/4 mile time 1          14 (43.75%)