R - Strings
Any value written within a pair of single quote or double quotes in R is treated as a string.
Internally R stores every string within double quotes, even when you create them with single
quote.
Rules Applied in String Construction
The quotes at the beginning and end of a string should be both double quotes or both single
quote. They can not be mixed.
Double quotes can be inserted into a string starting and ending with single quote.
Single quote can be inserted into a string starting and ending with double quotes.
Double quotes can not be inserted into a string starting and ending with double quotes.
Single quote can not be inserted into a string starting and ending with single quote.
Examples of Valid Strings
Following examples clarify the rules about creating a string in R.
Live Demo
a <- 'Start and end with single quote'
print(a)
b <- "Start and end with double quotes"
print(b)
c <- "single quote ' in between double quotes"
print(c)
d <- 'Double quotes " in between single quote'
print(d)
https://www.tutorialspoint.com/r/r_strings.htm 1/7
, 4/3/23, 11:16 PM R - Strings
When the above code is run we get the following output −
[1] "Start and end with single quote"
[1] "Start and end with double quotes"
[1] "single quote ' in between double quote"
[1] "Double quote \" in between single quote"
Examples of Invalid Strings
Live Demo
e <- 'Mixed quotes"
print(e)
f <- 'Single quote ' inside single quote'
print(f)
g <- "Double quotes " inside double quotes"
print(g)
When we run the script it fails giving below results.
Error: unexpected symbol in:
"print(e)
f <- 'Single"
Execution halted
String Manipulation
Concatenating Strings - paste() function
Many strings in R are combined using the paste() function. It can take any number of arguments
to be combined together.
Syntax
The basic syntax for paste function is −
https://www.tutorialspoint.com/r/r_strings.htm 2/7