Characters and Strings in R

Character strings are another common data type, used to represent text.

In R, character strings (or simply "strings") are indicated by double quotation marks. To create a string, just enter text between two paris of these quotes.

> myString = "This is a string of text!"
> myString
[1] "This is a string of text!"

> length(myString)     # the string is a single entity in a one-element vector
[1] 1

> nchar(myString)      # if you really want the number of characters that make up the string...
[1] 25

One can compare strings with the same relational operators used on numerical values. Less than and greater than determinations are made in accordance with the "dictionary order" of the strings (i.e., letters that come later in the alphabet are considered "greater" than those that come earlier; also, uppercase letters are "greater" than lowercase letters).

Most characters can be used in a string, with a couple of excpetions, one being the backslash character, "\". This character is called the escape character and is used to insert characters that would otherwise be difficult to add. For example, without an escape character, adding a double quote inside a string would pose a problem, as R would assume that you meant the string to end upon seeing the double quote. With an escape character, however, adding a double quote inside your string is easy, you simply prepend the double quote with the backslash. The table below shows some of the other characters that can be "escaped" in this way.

escape sequenceresutl
\nStarts a new line
\tHorizontal tab
\bBackspace
\"Double quote
\\Single Backslash