type.convert {utils}  R Documentation 
Convert Data to Appropriate Type
Description
Convert a data object to logical, integer, numeric, complex, character or factor as appropriate.
Usage
type.convert(x, ...)
## Default S3 method:
type.convert(x, na.strings = "NA", as.is, dec = ".",
numerals = c("allow.loss", "warn.loss", "no.loss"),
tryLogical = TRUE, ...)
## S3 method for class 'data.frame'
type.convert(x, ...)
## S3 method for class 'list'
type.convert(x, ...)
Arguments
x 
a vector, matrix, array, data frame, or list. 
na.strings 
a vector of strings which are to be interpreted as

as.is 
whether to store strings as plain 
dec 
the character to be assumed for decimal points. 
numerals 
string indicating how to convert numbers whose
conversion to double precision would lose accuracy, typically when

tryLogical 
a 
... 
arguments to be passed to or from methods. 
Details
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 when additionally as.is = FALSE
(no longer the default!), converts a character vector to
factor
. The first type that can accept all the nonmissing
values is chosen.
Vectors which are entirely missing values are converted to logical,
since NA
is primarily logical.
If tryLogical
is true as by default, vectors containing just
F
, T
, FALSE
, TRUE
and values from na.strings
are converted to logical. This may be surprising
in a context where you have many character
columns with e.g.,
1letter strings and you happen to get one with only "F"
. In such
cases tryLogical = FALSE
is recommended.
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 or (for
as.is = FALSE
) factors.
Since this is a helper function, the caller should always pass an
appropriate value of as.is
.
Value
An object like x
but using another storage mode when
appropriate.
Author(s)
R Core, with a contribution by Arni Magnusson
See Also
read.table
, class
,
storage.mode
.
Examples
## Numeric to integer
class(rivers)
x < type.convert(rivers, as.is = TRUE)
class(x)
## Convert many columns
auto < type.convert(mtcars, as.is = TRUE)
str(mtcars)
str(auto)
## Convert matrix
phones < type.convert(WorldPhones, as.is = TRUE)
storage.mode(WorldPhones)
storage.mode(phones)
## Factor or character
chr < c("A", "B", "B", "A")
ch2 < c("F", "F", "NA", "F")
(fac < factor(chr))
type.convert(chr, as.is = FALSE) # > factor
type.convert(fac, as.is = FALSE) # > factor
type.convert(chr, as.is = TRUE) # > character
type.convert(fac, as.is = TRUE) # > character
type.convert(ch2, as.is = TRUE) #> logical
type.convert(ch2, as.is = TRUE, tryLogical=FALSE) #> character