Chapter 14: R Strings
1. What is a String in R? (Simple Definition)
A string (or character vector element) is just text data — anything inside quotation marks.
In R:
- Single quotes ‘ ‘ or double quotes ” ” — both work perfectly
- The type is always called character (typeof() → “character”, class() → “character”)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
city <- "Hyderabad" food <- 'Biryani' year <- "2026" sentence <- "It's 5:50 PM in Hyderabad right now" typeof(city) # "character" is.character(food) # TRUE |
2. Creating Strings – All the Ways
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Single line – most common name <- "Webliance" city <- 'Secunderabad' # Multi-line string (very useful for long messages / SQL queries) long_text <- "Today we are learning about strings in R. This is line two. And this is line three." # Using paste() / paste0() / glue to build strings library(glue) # modern favorite – install once if needed greeting <- glue("Hello {name}! Welcome to {city} in {year}.") print(greeting) # "Hello Webliance! Welcome to Secunderabad in 2026." |
3. Escaping Special Characters (Very Important!)
Inside strings, some characters have special meaning — you must escape them with \
| Character you want | How to write it in string | Example |
|---|---|---|
| Double quote “ | \” | “He said \”Hello\”” |
| Single quote ‘ | \’ | ‘It\’s hot today’ |
| Backslash \ | \\ | “C:\\Users\\Webliance\\Documents” |
| New line | \n | “Line one\nLine two” |
| Tab | \t | “Name\tAge\tCity” |
Real example:
|
0 1 2 3 4 5 6 7 8 9 |
message <- "He said: \"It's 29.5°C in Hyderabad today!\"\nSee you tomorrow." cat(message) # He said: "It's 29.5°C in Hyderabad today!" # See you tomorrow. |
4. Most Important String Functions (Base R – No Package Needed)
| Function | What it does | Example code | Typical Output |
|---|---|---|---|
| nchar() | Length of string | nchar(“Hyderabad”) | 9 |
| paste() / paste0() | Combine strings | paste(“Temp”, 29.5, “°C”) | “Temp 29.5 °C” |
| paste(…, collapse=) | Join vector into one string | paste(c(“Mon”,”Tue”), collapse=”, “) | “Mon, Tue” |
| substr() / substring() | Extract substring | substr(“Hyderabad”, 1, 5) | “Hyder” |
| toupper() / tolower() | Change case | toupper(“Hyderabad”) | “HYDERABAD” |
| trimws() | Remove leading/trailing whitespace | trimws(” Hyderabad “) | “Hyderabad” |
| strsplit() | Split string by delimiter | strsplit(“A,B,C”, “,”)[[1]] | c(“A”,”B”,”C”) |
| grepl() | Check if pattern exists (regex) | grepl(“bad”, “Hyderabad”) | TRUE |
| gsub() / sub() | Replace pattern | gsub(“bad”, “good”, “Hyderabad”) | “Hyderagood” |
5. Modern 2026 Favorite: stringr Package (Highly Recommended!)
Most people now use stringr (part of tidyverse) instead of base functions — more consistent naming, better handling of NA, easier regex.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# install.packages("stringr") once library(stringr) str_length("Hyderabad") # 9 str_to_upper("Hyderabad") # "HYDERABAD" str_to_title("hyderabad is hot") # "Hyderabad Is Hot" str_detect("Hyderabad", "bad") # TRUE str_replace("Hyderabad", "bad", "good")# "Hyderagood" # Very powerful: str_c() like paste0 but smarter with NA str_c("Temp: ", c(28.5, NA, 30.2), "°C", sep = "") # "Temp: 28.5°C" NA "Temp: 30.2°C" |
6. Common Beginner Mistakes & Fixes
Mistake 1 — Forgetting quotes → R thinks it’s a variable name
|
0 1 2 3 4 5 6 7 8 9 10 |
# Wrong city <- Hyderabad # Error: object 'Hyderabad' not found # Correct city <- "Hyderabad" |
Mistake 2 — Mixing numbers and text without care
|
0 1 2 3 4 5 6 7 |
scores <- c(85, 92, "absent", 78) # whole vector becomes character! mean(scores) # Error! |
Fix: Keep numbers numeric, text as character, or use factors/data.frames for mixed data.
Mistake 3 — Not escaping quotes
|
0 1 2 3 4 5 6 7 8 9 10 |
# Wrong wrong <- "He said "Hello"" # syntax error # Correct correct <- "He said \"Hello\"" |
7. Your Mini Practice Right Now (Copy → Run!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Setup names <- c("Aarav", "Priya", "Rahul", "Sneha") cities <- c("Hyd", "Bng", "Del", "Mum") temps <- c(29.5, 28.8, 22.1, 31.2) # Build nice messages reports <- str_c( str_to_title(names), " from ", cities, " reported ", temps, "°C today" ) print(reports) # Count characters str_length(reports) # Extract first 3 letters of names str_sub(names, 1, 3) # Replace "Hyd" with full name str_replace(cities, "Hyd", "Hyderabad") # One long comma-separated list str_flatten(reports, collapse = " | ") |
You just used most of the important string operations!
Summary Cheat-Sheet (Keep This Handy)
- Create → “text” or ‘text’
- Escape → \”, \’, \\, \n, \t
- Length → nchar() or str_length()
- Combine → paste(), paste0(), str_c(), glue()
- Case → toupper(), tolower(), str_to_title()
- Replace → gsub(), str_replace()
- Detect → grepl(), str_detect()
- Modern choice → stringr or glue for clean code
Feeling confident with strings now?
Next step?
- Want to practice regex in R (very powerful for cleaning text)?
- How strings behave in data frames / dplyr?
- Or move to factors (special string type for categories)?
Just tell me — next lesson is ready! ☕📝🚀
