utils (version 3.6.2)

type.convert: Convert Data to Appropriate Type


Convert a data object to logical, integer, numeric, complex, character or factor as appropriate.


type.convert(x, …)
# S3 method for default
type.convert(x, na.strings = "NA", as.is = FALSE, dec = ".",
             numerals = c("allow.loss", "warn.loss", "no.loss"), …)
# S3 method for data.frame
type.convert(x, …)
# S3 method for list
type.convert(x, …)



a vector, matrix, array, data frame, or list.


a vector of strings which are to be interpreted as NA values. Blank fields are also considered to be missing values in logical, integer, numeric or complex vectors.


whether to convert factors to character vectors. The default is to convert character vectors to factors. See ‘Details’.


the character to be assumed for decimal points.


string indicating how to convert numbers whose conversion to double precision would lose accuracy, typically when x has more digits than can be stored in a double. Can be abbreviated. Possible values are

numerals = "allow.loss", default:

the conversion happens with some accuracy loss. This was the behavior of R versions 3.0.3 and earlier, and the default from 3.1.1 onwards.

numerals = "warn.loss":

a warning about accuracy loss is signalled and the conversion happens as with numerals = "allow.loss".

numerals = "no.loss":

x is not converted to a number, but to a factor or character, depending on as.is. This was the behavior of R version 3.1.0.

arguments to be passed to or from methods.


An object like x but using another storage mode when appropriate.


This helper function is used by read.table. When the data object x is a data frame or list, the function is called recursively for each column or list element.

Given a vector, the function attempts to convert it to logical, integer, numeric or complex, and failing that converts a character vector to factor unless as.is = TRUE. The first type that can accept all the non-missing values is chosen.

Vectors which are entirely missing values are converted to logical, since NA is primarily logical.

Vectors containing just F, T, FALSE, TRUE and values from na.strings are converted to logical. Vectors containing optional whitespace followed by decimal constants representable as R integers or values from na.strings are converted to integer. Other vectors containing optional whitespace followed by other decimal or hexadecimal constants (see NumericConstants), or NaN, Inf or infinity (ignoring case) or values from na.strings are converted to numeric. Where converting inputs to numeric or complex would result in loss of accuracy they can optionally be returned as strings (for as.is = TRUE) or factors.

Since this is a helper function, the caller should always pass an appropriate value of as.is.

See Also

read.table, class, storage.mode.


Run this code
## Numeric to integer
x <- type.convert(rivers)

## Convert many columns
auto <- type.convert(mtcars)

## Convert matrix
phones <- type.convert(WorldPhones)

## Factor or character
chr <- c("A", "B", "B", "A")
fac <- factor(c("A", "B", "B", "A"))
type.convert(chr)               # -> factor
type.convert(fac)               # -> factor
type.convert(chr, as.is = TRUE) # -> character
type.convert(fac, as.is = TRUE) # -> character
# }

Run the code above in your browser using DataLab