Rev 88581 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/character.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2026 R Core Team% Distributed under GPL 2 or later\name{character}\alias{character}\alias{as.character}\alias{as.character.default}\alias{as.character.factor}\alias{is.character}\title{Character Vectors}\description{Create or test for objects of type \code{"character"}.}\usage{character(length = 0)as.character(x, \dots)is.character(x)}\arguments{\item{length}{a non-negative integer specifying the desired length.Double values will be coerced to integer:supplying an argument of length other than one is an error.}\item{x}{object to be coerced or tested.}\item{\dots}{further arguments passed to or from other methods.}}\details{\code{as.character} is generic: you canwrite methods to handle specific classes of objects,see \link{InternalMethods}. Itsdefault method calls \code{\link{as.vector}}, so, onlyif \code{is.object(x)} is true, dispatch is first onmethods for \code{as.character} and then for methods for \code{as.vector}.\code{as.character} represents real and complex numbers to 15 significantdigits (technically the compiler's setting of the ISO C constant\code{DBL_DIG}, which will be 15 on machines supporting \abbr{IEC} 60559arithmetic according to the C99 standard). This ensures that all thedigits in the result will be reliable (and not the result ofrepresentation error), but does mean that conversion to character andback to numeric may change the number. If you want to convert numbersto character with the maximum possible precision, use\code{\link{format}}.}\value{\code{character} creates a character vector of the specified length.The elements of the vector are all equal to \code{""}.\code{as.character} attempts to coerce its argument to character type;like \code{\link{as.vector}} it strips attributes including names.For lists and pairlists (including \link{language objects} such ascalls) it deparses the elements individually, except that it extractsthe first element of length-one character vectors, see the \code{Abc}example.\code{is.character} returns \code{TRUE} or \code{FALSE} depending onwhether its argument is of character type or not.}\note{\code{as.character} breaks lines in language objects at 500characters, and inserts newlines. Prior to 2.15.0 lines weretruncated.}\seealso{\code{\link{options}}: options \code{scipen} and \code{OutDec} affect theconversion of numbers.\code{\link{paste}}, \code{\link{substr}} and \code{\link{strsplit}}for character concatenation and splitting,\code{\link{chartr}} for character translation and case folding (e.g.,upper to lower case) and \code{\link{sub}}, \code{\link{grep}} etc forstring matching and substitutions. Note that\code{help.search(keyword = "character")} gives even more links.\code{\link{deparse}}, which is normally preferable to\code{as.character} for \link{language objects}.\code{\link{Quotes}} on how to specify \code{character} / stringconstants, including \emph{raw} ones.}\references{\bibshow{R:Becker+Chambers+Wilks:1988}}\examples{form <- y ~ a + b + cas.character(form) ## length 3deparse(form) ## like the inputa0 <- 11/999 # has a repeating decimal representation(a1 <- as.character(a0))format(a0, digits = 16) # shows 1 to 2 more digit(s)a2 <- as.numeric(a1)a2 - a0 # normally around -1e-17as.character(a2) # possibly different from a1print(c(a0, a2), digits = 16)as.character(list(A = "Abc", xy = c("x", "y"))) # "Abc" "c(\"x\", \"y\")"## i.e., "Abc" directly instead of deparsing to "\"Abc\""}\keyword{character}\keyword{classes}