match_types: Coerce the columns of a data frame to the classes of a...

View source: R/io.R

match_typesR Documentation

Coerce the columns of a data frame to the classes of a template

Description

Useful after reading formats that do not store R classes (CSV, JSON, Excel): factors regain their levels (and ordering), dates and date-times are parsed, and integers/logicals are restored. Columns not present in the template are left unchanged.

Usage

match_types(x, template)

Arguments

x

A data frame.

template

A data frame with the desired column classes.

Details

Date-time strings ending in Z are read as UTC, strings with an explicit offset (e.g. +02:00) are converted from that offset, and strings without zone information are read as local times in the time zone of the template column.

Value

x with coerced columns.

Examples

f <- tempfile(fileext = ".csv")
write.csv(iris, f, row.names = FALSE)
x <- match_types(read.csv(f), iris)
identical(levels(x$Species), levels(iris$Species))

SynthesizerPlus documentation built on Oct. 16, 2026, 5:10 p.m.