R Character Data Type
The character data type stores text. Any value wrapped in single or double quotes in R becomes a character — also called a string. Names, addresses, category labels, and any other text in your data all live in character variables.
Creating Character Variables
city <- "Delhi" greeting <- 'Hello, World!' product_code <- "PRD-20048" empty_text <- "" # empty string — still valid class(city) # "character"
Single and double quotes work the same way. Use whichever feels natural, but stay consistent within a project. If your text itself contains a single quote, use double quotes around it (and vice versa):
message <- "It's a sunny day" title <- 'She said "Hello"'
Checking Length
Two different "length" concepts apply to character data:
phrase <- "Learning R" nchar(phrase) # 10 — number of characters in the string length(phrase) # 1 — number of elements (just one string here)
Concept Function Example Result
────────────────────────────────────────────────────
Characters in text nchar() nchar("Hello") → 5
Number of items length() length(c("a","b")) → 2
Combining Character Values
Use paste() to join text pieces together. By default it adds a space between pieces.
first <- "Ravi" last <- "Kumar" full <- paste(first, last) print(full) # "Ravi Kumar"
Use paste0() to join with no separator:
id <- paste0("EMP", 101)
print(id) # "EMP101"
Useful Character Functions
Function What It Does Example
───────────────────────────────────────────────────────────────────
toupper(x) Convert to UPPERCASE "delhi" → "DELHI"
tolower(x) Convert to lowercase "HELLO" → "hello"
nchar(x) Count characters "R" → 1
substr(x, s, e) Extract part of text substr("Hello",1,3) → "Hel"
gsub("old","new",x) Replace all occurrences gsub("a","@","banana")
trimws(x) Remove leading/trailing spaces " hi " → "hi"
strsplit(x, ",") Split text at delimiter "a,b,c" → list
A Practical Example: Name Formatter
raw_name <- " anjali sharma "
# Step 1: Remove extra spaces
clean_name <- trimws(raw_name)
# Step 2: Convert to title format (capitalize first letter of each word)
clean_name <- gsub("(\\w)(\\w*)", "\\U\\1\\L\\2", clean_name, perl = TRUE)
print(clean_name)
Output:
[1] "Anjali Sharma"
Checking and Converting Character
is.character("hello") # TRUE
is.character(42) # FALSE
as.character(99) # "99" — number to text
as.character(TRUE) # "TRUE"
as.numeric("3.14") # 3.14 — text to number (only if valid)
as.numeric("abc") # NA with warning
Character in Data Frames
Real datasets almost always mix numeric and character columns. For example, a customer table might look like:
Name (character) Age (numeric) City (character) ───────────────────────────────────────────────────── "Meera" 29 "Pune" "Arjun" 34 "Chennai" "Zara" 25 "Kolkata"
R handles character columns in data frames with the same functions — toupper(), nchar(), gsub() — applied across entire columns when needed.
Special Characters and Escape Sequences
Sequence Meaning Result in output ────────────────────────────────────────────────── \n New line moves to next line \t Tab horizontal space \\ Literal backslash \ \" Literal quote "
cat("Line 1\nLine 2\n")
# Line 1
# Line 2
cat("Name:\tPriya\n")
# Name: Priya
Character data appears in virtually every real dataset — whether it is product names, city labels, or user feedback. Learning to clean, combine, and transform character values is an essential skill in R data analysis.
