HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RVariablesData TypesOperatorsCharacters (character)VectorsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ Character
📚 Vector
🔀 Condition & Loop
🧩 Function
R›Characters (character)

Characters (character)

Length (nchar), uppercase/lowercase, searching (grepl) and replacing (gsub).

Simple enough for a 5-year-old to understand

Length and letter case

Example code
1
s <- "HexLibrary"
2
cat(nchar(s), "\n", sep="")
10 (character count)
Output / Input10 (character count)
3
cat(toupper(s), "\n", sep="")
HEXLIBRARY
Output / InputHEXLIBRARY
4
cat(tolower(s), "\n", sep="")
hexlibrary
Output / Inputhexlibrary
Output / Input
💡 Hint: Length is with nchar() (length() is the vector length).

About this topic

In R, texts are of character type; they are written between single or double quotes, and there is no difference in meaning between them. Texts are also stored as vectors, so most text functions are applied to all elements of a vector simultaneously.

The standard way to concatenate texts is the paste function. There is a common detail here: paste puts a space between parts by default. For concatenation without spaces, you either need to specify an empty separator or use paste0.

substr is used to extract a substring, grepl to search, and gsub to replace. The indices of these functions also start at one; the rule that applies everywhere in R also applies to texts.

What you will learn

  • Creating and concatenating strings
  • the difference between paste and paste0
  • Measuring length and extracting substrings
  • Search and replacement operations
  • Generating formatted output

Who is this for?

  • Those who will perform data cleaning
  • Those preparing report and chart labels
  • Those who need to organize text fields

When to use?

Column names, category labels, and free-text fields in raw datasets almost always require cleaning; text functions are the first step of analysis.

Chart titles and report sentences are generated by combining with variable values; formatted output functions make the job easier here.

Common mistakes

Wrong

ad <- "Ayse"
mesaj <- paste("Hello", ad, "!")
print(mesaj)

because paste inserts a space between parts by default, the result becomes "Hello Ayse !"; an unwanted space remains before the exclamation mark. You need to control the separator or group the parts correctly.

Correct

ad <- "Ayse"
mesaj <- paste0("Hello ", ad, "!")
print(mesaj)

Wrong

metin <- "hello"
print(length(metin))

length gives the number of elements in a vector; for a vector consisting of a single text, the result is 1, not 7. nchar is used to find the number of characters. Confusing these two is one of the most common mistakes in R.

Correct

metin <- "hello"
print(nchar(metin))

Wrong

metin <- "hello"
print(substr(metin, 0, 3))

Indices in R start at one; zero is not a valid starting point and you get a shorter result than expected. Those coming from other languages are used to counting from zero, and this difference shows itself everywhere in R.

Correct

metin <- "hello"
print(substr(metin, 1, 3))

Frequently asked questions

What is the difference between paste and paste0?

paste puts a space between parts by default, paste0 puts nothing. paste0 is the right choice when generating file paths or IDs; paste's space is useful when forming sentences.

How do I split a text into parts?

The strsplit function splits according to the separator you specify and returns a list as a result. If you are working with a single text, you need to take the first element of the result; this detail can be surprising on first use.

How do I print formatted texts?

The sprintf function produces formatted output with placeholders; you can control the number of decimal places and alignment. It gives much more readable results than paste in report outputs and chart labels.

Summary

Texts are of character type, quote type doesn't matter.

paste adds a space, paste0 does not.

Character count is obtained with nchar, not length.

Text indices start at one.

sprintf produces formatted output.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code