# Print a string directly to the console
"This is a string or character"[1] "This is a string or character"
“Hello world!”
The first exercise in programming is printing text to the console. While this is trivial in languages like R, printing text in languages such as Fortran, C, or C++ can feel like an odyssey for an absolute beginner. When one finally manages to understand the computer’s logic, figuratively taming and bringing the machine into the world of the living, the very first order is to ask it to greet: “Hello world”.
A computer program is more than a basic calculator, which should be clear by now. A major feature of a computer program is its ability to handle non-numeric data. Such objects are called strings, which is why printing “Hello world” involves string manipulation.
Unlike numbers, arithmetic operations cannot be performed on strings—letters or words cannot be multiplied or divided. Manipulating strings requires non-mathematical operations that are natural to human language, such as deleting a letter or word, combining text, or replacing text.
For most ecological applications and the purpose of this handbook, strings represent qualitative variables (Chapter 18). For example, entries such as “male” or “female” are treated as strings, in contrast to numeric measurements like height or weight.
A string or character vector is an object type used to represent text, defined by enclosing content within quotation marks ("" or '').
# Print a string directly to the console
"This is a string or character"[1] "This is a string or character"
When a string is displayed in the console, R includes quotation marks to indicate its character data type.
# Inspect the structure of a character scalar
str("a") chr "a"
The character type is abbreviated as chr in structure outputs generated by str().
Assigning a character to a variable
# A character object containing a single letter
one_letter <- "R"
# A character object containing a single word
a_word <- "Hello"
# A character object containing a sentence
long_string <- "Ny akoho tsy nanidina tsy nahandry ny andro zoma."Any special character can be included inside a string
"There is no problem in usin$ spécial character in a string, & < is not ! or 1 2 3"[1] "There is no problem in usin$ spécial character in a string, & < is not ! or 1 2 3"
Performing arithmetic operations on string objects produces an error because mathematical operators are not defined for character data:
# Defined as a character string, not a numeric value
string_1 <- "1"
# Defined as a character string, not a numeric value
string_2 <- "2026"
#Attempting addition on character strings triggers an error
string_1 + string_2Error in string_1 + string_2: non-numeric argument to binary operator
"Magadascar" is a specific sequence of letters and differs from "Madagascar", just as 12 differs from 21. While order differences in numbers are easily spotted, typographical errors in text are harder to detect. Typos are unavoidable.
Lingering spaces: entering data in spreadsheet software like MS Excel or graphical interfaces frequently introduces subtle errors. Beyond capitalization typos (e.g., "male" vs. "Male"), a hidden typo that remains unnoticed in spreadsheet software is a trailing space like "male ". Data cleaning is therefore necessary when importing datasets into R1.
Distinguishing between three distinct concepts that share the same name
# A string literal composed of three characters
"sin"[1] "sin"
# A variable named sin storing a character string
sin <- "sin"
# A variable named siin storing the value 2
siin <- 2
# The trigonometric sine function, identified by trailing parentheses () computing the sine of the variable siin
sin(siin)[1] 0.9092974
Combine strings using the function paste() and specify a delimiter using the sep argument, such as an empty string "", a comma ",", a comma followed by a space ", ", or any arbitrary character sequence:
# Combine strings with no separation
paste("Hello", "world", sep = "")[1] "Helloworld"
# Combine strings with a comma separator
paste("Hello", "world", sep = ",")[1] "Hello,world"
# Combine strings with a comma and space separator
paste("Hello", "world", sep = ", ")[1] "Hello, world"
# Combine strings using a custom text delimiter
paste("Hello", "world", sep = " anything you want ")[1] "Hello anything you want world"
The paste() function accepts multiple string arguments and works with variable names in place of raw string values:
# Combine three strings with a custom separator
paste("Hello", "world", "life", sep = " there ")[1] "Hello there world there life"
# Assign string values to variables
word1 <- "Hello"
word2 <- "world"
# Combine variables; default separator is a single space " "
paste(word1, word2, word1, word2)[1] "Hello world Hello world"
String is the universal term in programming, whereas character is the standard term used in R. A third object type, called a factor, also exists in R.2
In R, double quotation marks "" and single quotation marks '' can be used interchangeably. Some R practitioners prefer single quotes, whereas multi-language programmers often prefer "" to maintain consistent syntax across languages.
covered in a subsequent volume↩︎
Factors are better illustrated later alongside vectors (Chapter 35).↩︎