dt_set_logical2factor | R Documentation |
Convert data.table logical columns to factor with custom labels in-place
dt_set_logical2factor(
x,
cols = NULL,
labels = c("False", "True"),
maintain_attributes = TRUE,
fillNA = NULL
)
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 |
E.D. Gennatas
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.