String in R programming Language

Rumman Ansari   Software Engineer   2023-03-24   6138 Share
☰ Table of Contents

Table of Content:


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.

 

m <- 'Start and end with single quote'
print(m)

n <- "Start and end with double quotes"
print(n)

p <- "single quote ' in between double quotes"
print(p)

q <- 'Double quotes " in between single quote'
print(q)

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"