character {base} | R Documentation |
Character Vectors
Description
Create or test for objects of type "character"
.
Usage
character(length = 0)
as.character(x, ...)
is.character(x)
Arguments
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. |
x |
object to be coerced or tested. |
... |
further arguments passed to or from other methods. |
Details
as.character
and is.character
are generic: you can
write methods to handle specific classes of objects,
see InternalMethods. Further, for as.character
the
default method calls as.vector
, so, only
if(is.object(x))
is true, dispatch is first on
methods for as.character
and then for methods for as.vector
.
as.character
represents real and complex numbers to 15 significant
digits (technically the compiler's setting of the ISO C constant
DBL_DIG
, which will be 15 on machines supporting IEC 60559
arithmetic according to the C99 standard). This ensures that all the
digits in the result will be reliable (and not the result of
representation error), but does mean that conversion to character and
back to numeric may change the number. If you want to convert numbers
to character with the maximum possible precision, use
format
.
Value
character
creates a character vector of the specified length.
The elements of the vector are all equal to ""
.
as.character
attempts to coerce its argument to character type;
like as.vector
it strips attributes including names.
For lists and pairlists (including language objects such as
calls) it deparses the elements individually, except that it extracts
the first element of length-one character vectors, see the Abc
example.
is.character
returns TRUE
or FALSE
depending on
whether its argument is of character type or not.
Note
as.character
breaks lines in language objects at 500
characters, and inserts newlines. Prior to 2.15.0 lines were
truncated.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
See Also
options
: options scipen
and OutDec
affect the
conversion of numbers.
paste
, substr
and strsplit
for character concatenation and splitting,
chartr
for character translation and case folding (e.g.,
upper to lower case) and sub
, grep
etc for
string matching and substitutions. Note that
help.search(keyword = "character")
gives even more links.
deparse
, which is normally preferable to
as.character
for language objects.
Quotes
on how to specify character
/ string
constants, including raw ones.
Examples
form <- y ~ a + b + c
as.character(form) ## length 3
deparse(form) ## like the input
a0 <- 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-17
as.character(a2) # possibly different from a1
print(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\""