knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

options(rmarkdown.html_vignette.check_title = FALSE)
library(infra)

Assigning Variable Types

The function type can be used to assign the type attribute to a single vector, a column in a data.frame, of multiple columns of a data.frame. There are a couple ways to use the function depending on the object that the type attribute is being assigned.

The following is a list of the type's that are available. If a type other than these is used, and error is returned.

Assign type to vector

foo <- rnorm(20)

type(foo) <- "x"
type(foo)

type(foo) <- "error"

Assign type to a single data.frame column.

type(test$usubjid) <- "id"
type(test$usubjid)

Assign type to data.frame

Assign to entire data.frame

value is assigned by the index of value to the corresponding column of x. There are alternative approaches as well.

test2 <- test[, 1:6]

type(test2) <- c("id", "id", "id", "x", "x", "x")
type(test2)

Using a named vector

set.seed(4)
vnames <- sample(names(test), 6)
v <- c("x", "id", "x", "y", "x", "x")
v <- setNames(v, vnames)
v

test2 <- test[, vnames]
type(test2) <- v
type(test2)

Using the fill option in the method for data.frame

In order for the type function to work on a data.frame, a type must be assigned to every column. Thus the parameters in data.frame method for type must be that length(value) == ncol(x), unless the fill parameter is used. Using the fill parameter allows you to specify one variable type that is assigned to all the columns which are not explicitly named.

v
type(test, fill = "x") <- v
type(test)

NA can also be used as a fill value to exclude unused variables.

type(test, fill = NA_character_) <- v
type(test)


JamesCuster/BDSHInfra documentation built on May 12, 2021, 4:42 a.m.