R Strings
A string holds text. Quotes mark the start and the end of the text.
Quotes for text
The prints show text in double quotes and in single quotes. The last string uses double quotes around a word with an apostrophe.
print("Hello")
print('World')
print("it's")Counting characters
nchar counts the characters in each string. The counts include the space in the first string.
print(nchar("Hello World"))
print(nchar("abc"))Length of one string
length gives 1 for a single string while nchar gives 3. The last print shows a string with a tab escape.
print(length("abc"))
print(nchar("abc"))
print("a\tb")Example
greeting <- "Hello World"
print(greeting)
print(nchar(greeting))
print(length(greeting))Key points
- Text goes inside double quotes or single quotes.
- Both quote styles make the same kind of string.
- nchar counts the characters in a string.
- length counts how many strings sit in a value.
- A single string has a length of 1 even when it holds many characters.
- An escape such as \t stands for a tab character inside a string.
In this example, the greeting string is printed first. nchar counts 11 characters in it. length shows the value holds one string.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท rdrr.io R Snippets
Exercise
What does print(length("abc")) show?