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 exceptions, 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 sequenceresult
\nStarts a new line
\tHorizontal tab
\"Double quote
\\Single Backslash

If one needs to convert a numerical value to a string, one can use the "paste" function, as shown below:

> paste(5)
[1] "5"

This function can also be used to concatenate corresponding elements of vectors containing strings, as shown here:

> paste(c("A","B","C"),c("1","2","3"))
[1] "A 1" "B 2" "C 3"

Notice in the example above, a single space was inserted between each letter and number. If one prefers to concatenate the elements with no space between them, one can use the paste0() function instead, as the following suggests.

> paste0(c("A","B","C"),c("1","2","3"))
[1] "A1" "B2" "C3"