dt_set_autotypes: Set column types automatically

View source: R/utils_data.table.R

dt_set_autotypesR Documentation

Set column types automatically

Description

This function inspects a data.table and attempts to identify columns that should be numeric but have been read in as character, and fixes their type in-place. This can happen when one or more fields contain non-numeric characters, for example.

Usage

dt_set_autotypes(x, cols = NULL, verbosity = 1L)

Arguments

x

data.table: Input data.table. Will be modified in-place, if needed.

cols

Character vector: columns to work on. If not defined, will work on all columns

verbosity

Integer: Verbosity level.

Value

data.table, invisibly.

Author(s)

EDG

Examples

library(data.table)
x <- data.table(
  id = 8001:8006,
  a = c("3", "5", "undefined", "21", "4", NA),
  b = c("mango", "banana", "tangerine", NA, "apple", "kiwi"),
  c = c(1, 2, 3, 4, 5, 6)
)
str(x)
# ***in-place*** operation means no assignment is needed
dt_set_autotypes(x)
str(x)

# Try excluding column 'a' from autotyping
x <- data.table(
  id = 8001:8006,
  a = c("3", "5", "undefined", "21", "4", NA),
  b = c("mango", "banana", "tangerine", NA, "apple", "kiwi"),
  c = c(1, 2, 3, 4, 5, 6)
)
str(x)
# exclude column 'a' from autotyping
dt_set_autotypes(x, cols = setdiff(names(x), "a"))
str(x)

rtemis documentation built on Oct. 20, 2026, 1:06 a.m.