utils_as: Encode variables to a specific format

Description Usage Arguments Value Author(s) Examples

Description

[Stable]

Function to quick encode vector or columns to a specific format.

Usage

1
2
3
4
5
6
7
8
9
as_numeric(.data, ..., .keep = "all", .pull = FALSE)

as_integer(.data, ..., .keep = "all", .pull = FALSE)

as_logical(.data, ..., .keep = "all", .pull = FALSE)

as_character(.data, ..., .keep = "all", .pull = FALSE)

as_factor(.data, ..., .keep = "all", .pull = FALSE)

Arguments

.data

A data frame or a vector.

...

<tidy-select>. If .data is a data frame, then ... are the variable(s) to encode to a format.

.keep

Allows you to control which columns from .data are retained in the output.

  • "all" (default) retains all variables.

  • "used" keeps any variables used to make new variables.

.pull

Allows you to pull out the last column of the output. It is useful in combination with .keep = "used". In this case, a vector will be created with the used column.

Value

An object of the same class of .data with the variables in ... encoded to the specified format.

Author(s)

Tiago Olivoto tiagoolivoto@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(metan)
library(tibble)
df <-
  tibble(y = rnorm(5),
         x1 = c(1:5),
         x2 = c(TRUE, TRUE, FALSE, FALSE, FALSE),
         x3 = letters[1:5],
         x4 = as.factor(x3))
df

# Convert y to integer
as_integer(df, y)
as_integer(df$y)

# convert x3 to factor
as_factor(df, x3)

# Convert all columns to character
as_character(df, everything())

# Convert x2 to numeric and coerce to a vector
as_numeric(df, x2, .keep = "used", .pull = TRUE)

metan documentation built on Nov. 10, 2021, 9:11 a.m.