mimic.default: Try To Look Like Another Equal-length Variable

View source: R/mimic.R

mimic.defaultR Documentation

Try To Look Like Another Equal-length Variable

Description

Tries to mimic another vector or factor. If meaningful and possible, x acquires a guide attribute with labels from corresponding values in y. Any codelist attribute is removed. No guide is created for zero-length x. If x is a factor, unused levels are removed.

Usage

## Default S3 method:
mimic(x, y = x, ...)

Arguments

x

vector-like

y

vector-like, same length as x

...

passed to link{factor}

Value

same class as x

See Also

Other mimic: mimic.classified(), mimic()

Other interface: canonical.decorated(), classified.data.frame(), decorate.character(), decorate.data.frame(), desolve.decorated(), ggplot.decorated(), io_csv.character(), io_csv.data.frame(), io_res.character(), io_res.decorated(), io_table.character(), io_table.data.frame(), io_yamlet.character(), io_yamlet.data.frame(), is_parseable.default(), modify.default(), promote.list(), read_yamlet(), resolve.decorated(), selected.default(), write_yamlet()

Examples

library(magrittr)
library(dplyr)
let <- letters[1:5]
LET <- LETTERS[1:5]
int <- 0L:4L
num <- as.numeric(int)
fac <- factor(let)
css <- classified(let)

# any of these can mimic any other
str(mimic(LET, let))
str(mimic(num, let))
str(mimic(let, num))

# factors get a guide and classifieds get a named codelist
str(mimic(fac, int))
str(mimic(css, int))

# int can 'pick up' the factor levels as guide names
str(mimic(int, css))

# if two variables mean essentially the same thing,
# mimic lets you save space
x <- data.frame(id = 1:2, ID = c('A','B'))
x
x %<>% mutate(id = mimic(id, ID)) %>% select(-ID)
x
# ID still available, in principle:
x %>% as_decorated %>% resolve

yamlet documentation built on Oct. 6, 2023, 9:07 a.m.