lossless_convert: Convert the class of a vector to another class - when...

lossless_convertR Documentation

Convert the class of a vector to another class – when possible without introducing additional NAs

Description

Convert the class of a vector to another class – when possible without introducing additional NAs. If NAs would be introduced, the original vector will be returned along with a warning so the user knows it has not been converted.

Usage

lossless_convert(x, class)

Arguments

x

vector of indeterminate length and type

class

character vector of length one specifying the preferred new column type (i.e., 'character', 'numeric', 'integer', or 'factor')

Value

a vector of the same length as x, but of the new class (when possible)

Examples


str(lossless_convert(c('1', '2', '3'), 'integer'))
str(lossless_convert(c('one', '2', '3'), 'integer'))
str(lossless_convert(c('1', '2', 'three'), 'integer'))

str(lossless_convert(c('2020-01-01', '2021-12-31', '2022-02-22'), 'Date'))
str(lossless_convert(c('2020-01-01', '2021-12-31', 'z'), 'Date'))
str(lossless_convert(c('z', '2020-01-01', '2021-12-31'), 'Date'))



PHSKC-APDE/rads documentation built on April 14, 2025, 10:47 a.m.