This function converts a data.frame
or a tibble
into a safeframe
object, where data are labelled and validated. The output will seem to be the
same data.frame
, but safeframe
-aware packages will then be able to
automatically use labelled fields for further data cleaning and analysis.
Arguments
- x
a
data.frame
or atibble
- ...
<
dynamic-dots
> A named list with variable names inx
as list names and the labels as list values. Values set toNULL
remove the label. When specifying labels, please also seedefault_values
.
See also
An overview of the safeframe package
labels()
: for a list of labelled variables in asafeframe
set_labels()
: for modifying labelslabels_df()
: for selecting variables by labels
Examples
x <- make_safeframe(cars,
speed = "Miles per hour",
dist = "Distance in miles"
)
## print result - just first few entries
head(x)
#>
#> // safeframe object
#> speed dist
#> 1 4 2
#> 2 4 10
#> 3 7 4
#> 4 7 22
#> 5 8 16
#> 6 9 10
#>
#> labelled variables:
#> speed - Miles per hour
#> dist - Distance in miles
## check labels
labels(x)
#> $speed
#> [1] "Miles per hour"
#>
#> $dist
#> [1] "Distance in miles"
#>
## Labels can also be passed as a list with the splice operator (!!!)
my_labels <- list(
speed = "Miles per hour",
dist = "Distance in miles"
)
new_x <- make_safeframe(cars, !!!my_labels)
## The output is strictly equivalent to the previous one
identical(x, new_x)
#> [1] TRUE