dt_set_logical2factor: Convert data.table logical columns to factor with custom...

View source: R/dt_ops.R

dt_set_logical2factorR Documentation

Convert data.table logical columns to factor with custom labels in-place

Description

Convert data.table logical columns to factor with custom labels in-place

Usage

dt_set_logical2factor(
  x,
  cols = NULL,
  labels = c("False", "True"),
  maintain_attributes = TRUE,
  fillNA = NULL
)

Arguments

x

data.table

cols

Integer or character: columns to convert, if NULL, operates on all logical columns

labels

Character: labels for factor levels

maintain_attributes

Logical: If TRUE, maintain column attributes

fillNA

Character: If not NULL, fill NA values with this constant

Author(s)

E.D. Gennatas

Examples

## Not run: 
library(data.table)
x <- data.table(a = 1:5, b = c(T, F, F, F, T))
x
dt_set_logical2factor(x)
x
z <- data.table(alpha = 1:5, beta = c(T, F, T, NA, T), gamma = c(F, F, T, F, NA))
z
# You can usee fillNA to fill NA values with a constant
dt_set_logical2factor(z, cols = "beta", labels = c("No", "Yes"), fillNA = "No")
z
w <- data.table(mango = 1:5, banana = c(F, F, T, T, F))
w
dt_set_logical2factor(w, cols = 2, labels = c("Ugh", "Huh"))
w
# Column attributes are maintained by default:
z <- data.table(alpha = 1:5, beta = c(T, F, T, NA, T), gamma = c(F, F, T, F, NA))
for (i in seq_along(z)) setattr(z[[i]], "source", "Guava")
str(z)
dt_set_logical2factor(z, cols = "beta", labels = c("No", "Yes"))
str(z)

## End(Not run)

egenn/rtemis documentation built on Oct. 28, 2024, 6:30 a.m.