Convert a data object to logical, integer, numeric, complex, character or factor as appropriate.
1 2 3 4 5 6 7 8
a vector, matrix, array, data frame, or list.
a vector of strings which are to be interpreted as
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
arguments to be passed to or from methods.
This helper function is used by
read.table. When the
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,
NA is primarily logical.
Vectors containing just
and values from
na.strings are converted to logical. Vectors
containing optional whitespace followed by decimal constants
representable as R integers or values from
converted to integer. Other vectors containing optional whitespace
followed by other decimal or hexadecimal constants (see
(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
An object like
x but using another storage mode when
R Core, with a contribution by Arni Magnusson
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
## Numeric to integer class(rivers) x <- type.convert(rivers) class(x) ## Convert many columns auto <- type.convert(mtcars) str(mtcars) str(auto) ## Convert matrix phones <- type.convert(WorldPhones) storage.mode(WorldPhones) storage.mode(phones) ## 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.