Introduction

This vignette shows you how to add labels to the columns of a data set.

Set up

knitr::opts_chunk$set(comment = '.')
library(purrr)
library(dplyr)
library(yspec)

Load specification object and data set

We'll use the examples provided in the package

data <- ys_help$data()
spec <- ys_help$spec()

The data

as_tibble(data)

The spec

spec

Use ys_add_labels

data <- ys_add_labels(data,spec)

It isn't obvious that anything was done here

as_tibble(data)

How can you tell that the labels were added?

labs <- map(data, attr, "label")

labs[1:5]

Or do this

str(data)

Where does label come from?

Ideally, we'd like to be writing in a label entry for every column in the data set. You can set the ys.require.label option to TRUE to require this when loading the spec (an error will be generated).

But yspec has a function called ys_get_label that will form a label for you. Here are the rules:

  1. If label exists for a column, it will be used
  2. Otherwise, if long is found and it is <= 40 characters, it be used
  3. Otherwise, short will be used; reminder that short defaults to the column name (col) too

Let's look at some examples:

ys_get_label(spec)[1:3]
ys_get_label(spec$NUM)
spec$NUM$label
spec$C$label

Custom label formation

Just as an example, we can add a custom labeling function. For example, I want the label to be the column name.

Set up a function that takes the column data as the first argument

label_fun <- function(x,...) x[["col"]]

Now, pass that function into ys_add_labels

data <- ys_add_labels(data, spec, fun = label_fun)

And check the output

map(data, attr, "label")[1:5]

Extract the label field

Recall that the yspec object is just a list. We can always map across that list and grab the label field:s

map(spec, "label")[1:5]


metrumresearchgroup/yspec documentation built on May 24, 2024, 12:48 a.m.