Rev 83360 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/utils/man/type.convert.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2022 R Core Team% Distributed under GPL 2 or later\name{type.convert}\alias{type.convert}\alias{type.convert.default}\alias{type.convert.list}\alias{type.convert.data.frame}\title{Convert Data to Appropriate Type}\description{Convert a data object to logical, integer, numeric, complex, characteror factor as appropriate.}\usage{type.convert(x, \dots)\method{type.convert}{default}(x, na.strings = "NA", as.is, dec = ".",numerals = c("allow.loss", "warn.loss", "no.loss"),tryLogical = TRUE, \dots)\method{type.convert}{data.frame}(x, \dots)\method{type.convert}{list}(x, \dots)}\arguments{\item{x}{a vector, matrix, array, data frame, or list.}\item{na.strings}{a vector of strings which are to be interpreted as\code{\link{NA}} values. Blank fields are also considered to bemissing values in logical, integer, numeric or complex vectors.}\item{as.is}{whether to store strings as plain \code{character}. Whenfalse, convert character vectors tofactors. See \sQuote{Details}.}\item{dec}{the character to be assumed for decimal points.}\item{numerals}{string indicating how to convert numbers whoseconversion to double precision would lose accuracy, typically when\code{x} has more digits than can be stored in a \code{\link{double}}.Can be abbreviated. Possible values are\describe{\item{\code{numerals = "allow.loss"}, default:}{the conversionhappens with some accuracy loss.}\item{\code{numerals = "warn.loss"}:}{a \code{\link{warning}}about accuracy loss is signalled and the conversion happens aswith \code{numerals = "allow.loss"}.}\item{\code{numerals = "no.loss"}:}{\code{x} is \emph{not}converted to a number, but to a \code{\link{factor}} or\code{character}, depending on \code{as.is}.}}}\item{tryLogical}{a \code{\link{logical}} determining if vectorsconsisting entirely of \code{F}, \code{T}, \code{FALSE}, \code{TRUE}and \code{na.strings} should be converted to \code{\link{logical}};true, by default.}\item{\dots}{arguments to be passed to or from methods.}}\details{This helper function is used by \code{\link{read.table}}. When thedata object \code{x} is a data frame or list, the function is calledrecursively for each column or list element.Given a vector, the function attempts to convert it to logical,integer, numeric or complex, and when additionally \code{as.is = FALSE}(no longer the default!), converts a character vector to\code{\link{factor}}. The first type that can accept all the non-missingvalues is chosen.Vectors which are entirely missing values are converted to logical,since \code{NA} is primarily logical.If \code{tryLogical} is true as by default, vectors containing just\code{F}, \code{T}, \code{FALSE}, \code{TRUE}and values from \code{na.strings} are converted to logical. This may be surprisingin a context where you have many \code{character} columns with e.g.,1-letter strings and you happen to get one with only \code{"F"}. In suchcases \code{tryLogical = FALSE} is recommended.%Vectors containing optional whitespace followed by decimal constantsrepresentable as \R integers or values from \code{na.strings} areconverted to integer. Other vectors containing optional whitespacefollowed by other decimal or hexadecimal constants (see\link{NumericConstants}), or \code{NaN}, \code{Inf} or \code{infinity}(ignoring case) or values from \code{na.strings} are converted tonumeric. Where converting inputs to numeric or complex would resultin loss of accuracy they can optionally be returned as strings or (for\code{as.is = FALSE}) factors.Since this is a helper function, the caller should always pass anappropriate value of \code{as.is}.}\value{An object like \code{x} but using another storage mode whenappropriate.}\author{R Core, with a contribution by Arni Magnusson}\seealso{\code{\link{read.table}}, \code{\link{class}},\code{\link{storage.mode}}.}\examples{## Numeric to integerclass(rivers)x <- type.convert(rivers, as.is = TRUE)class(x)## Convert many columnsauto <- type.convert(mtcars, as.is = TRUE)str(mtcars)str(auto)## Convert matrixphones <- type.convert(WorldPhones, as.is = TRUE)storage.mode(WorldPhones)storage.mode(phones)## Factor or characterchr <- c("A", "B", "B", "A")ch2 <- c("F", "F", "NA", "F")(fac <- factor(chr))type.convert(chr, as.is = FALSE) # -> factortype.convert(fac, as.is = FALSE) # -> factortype.convert(chr, as.is = TRUE) # -> charactertype.convert(fac, as.is = TRUE) # -> charactertype.convert(ch2, as.is = TRUE) #-> logicaltype.convert(ch2, as.is = TRUE, tryLogical=FALSE) #-> character}\keyword{manip}