View source: R/utils_data.table.R
| dt_set_logical2factor | R Documentation |
Convert data.table logical columns to factors with custom labels in-place
dt_set_logical2factor(
x,
cols = NULL,
labels = c("False", "True"),
maintain_attributes = TRUE,
fillNA = NULL
)
x |
data.table: Input data.table. Will be modified in-place. |
cols |
Optional 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 |
Optional Character: If not NULL, fill NA values with this constant. |
data.table, invisibly.
EDG
library(data.table)
x <- data.table(a = 1:5, b = c(TRUE, FALSE, FALSE, FALSE, TRUE))
x
dt_set_logical2factor(x)
x
z <- data.table(
alpha = 1:5,
beta = c(TRUE, FALSE, TRUE, NA, TRUE),
gamma = c(FALSE, FALSE, TRUE, FALSE, NA)
)
# 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(FALSE, FALSE, TRUE, TRUE, FALSE))
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(TRUE, FALSE, TRUE, NA, TRUE),
gamma = c(FALSE, FALSE, TRUE, FALSE, 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.