character
Character Objects

Description

Creates or tests for objects of mode character.

Usage

character(length = 0L) 
is.character(x) 
as.character(x, ...)

Arguments

length integer giving the length of the returned object.
... additional arguments passed to the generic method.
x any S object.

Details

is.character is not generic.
as.character is generic and has a method implemented for class factor. The default method of as.character calls as.vector(x, mode="character"). Note that as.vector is generic.
Simple objects have no attributes. Data elements of objects of mode "character" are character strings. In most R expressions, it is not necessary to ensure explicitly that data are of a particular mode. For example, the function paste does not need character arguments; it coerces data to character as needed.
Note the difference between coercing to a simple object of mode "character" and setting the mode attribute:
mode(myobject) <- "character"
This example changes the mode of myobject but leaves all other attributes unchanged. (For example, a matrix stays a matrix.)
Note that the value of as.character(myobject) has no attributes.
If you use ascii codes in a character string, "\n" denotes an ascii newline character and "\t" denotes an ascii tab character. "\\" denotes a backslash, and "\"" represents a quote within a string. Some other C escape sequences are allowed but are not supported. Arbitrary ascii codes might be included by "\nnn", where nnn is a 3-digit number in octal notation (for example, "\012" denotes "\n").
Value

characterreturns a character vector of the length specified containing null strings ("").
is.characterreturns TRUE if x has mode "character". Otherwise, it returns FALSE. Its behavior is unaffected by any attributes of x. For example, this function returns TRUE if x is a character array (in contrast to the behavior of is.vector).
as.characterreturns x if x is a simple object of mode "character". Otherwise, it returns a character vector of the same length as x, with data resulting from coercing the elements of x to mode "character".
See Also
paste, format, substring, deparse, nchar, mode.
Examples
character(5)  #   a character object with length 5: "", "", "", "","" 
as.character(1:10)  #    character representations of 1,2,...,10 

x <- matrix(c("a","b","c","d"), 2) is.character(x) # TRUE, since the mode of x is character though its class is matrix

as.character(x) # "a","b","c","d"

x <- factor(c("a","b","c","a")) # Examples for a factor is.character(x) # FALSE, since the mode of x is numeric for a factor as.character(x) # "a","b","c","a"

x <- ordered(c("a","b","c","a")) # Examples for an ordered factor is.character(x) # FALSE, since the mode of x is numeric for an ordered factor

as.character(x) # "a","b","c","a"

Package base version 6.1.1-7
Package Index