Basic String
Functions in R
Learn to combine, extract, measure, and search text using R's essential string functions: paste, substr, nchar, and grep/grepl.
What Are Strings?
In R, a string (also called a character value) is any piece of text wrapped in quotation marks. Strings let you store names, sentences, file paths, labels, and more.
Why String Functions Matter
Real-world data is messy. Names arrive in separate columns, dates need reformatting, and you often need to search text for patterns. String functions are your essential toolkit for wrangling text data.
The Five Core Functions
paste() and paste0()
paste() joins strings together with a separator (default is a space). paste0() does the same but with no separator at all.
Measuring Length with nchar()
nchar() counts the number of characters in a string, including spaces and punctuation. While length() tells you how many strings are in a vector, nchar() tells you how many characters are in each of those strings.
Extracting with substr()
substr(x, start, stop) extracts a substring from position start to position stop (inclusive on both ends). R uses 1-based indexing.
Searching with grep() and grepl()
grep() returns the indices of elements that match a pattern. grepl() returns a logical vector of TRUE/FALSE for each element.
paste() vs paste0(): Key Difference
| paste() | paste0() | |
|---|---|---|
| Default separator | " " (space) | "" (nothing) |
| sep argument | Supported | Not used |
| paste("A","B") | "A B" | "AB" |
| collapse argument | Supported | Supported |
| Equivalent to | paste(..., sep=" ") | paste(..., sep="") |
grep() vs grepl(): Which to Use?
| grep() | grepl() | |
|---|---|---|
| Returns | Integer indices | Logical (TRUE/FALSE) |
| Best for | Extracting positions | Filtering / subsetting |
| Use with if() | Needs \(length() > 0\) | Works directly |
| value=TRUE | Returns matching strings | Not applicable |
Step-by-Step Approach
Example 1: Building File Names
You have a vector of IDs and need to create file names like "data_001.csv".
Example 2: Extracting Area Codes
You have phone numbers as strings and want to extract the 3-digit area code from the beginning.
Example 3: Filtering with grepl()
You have a list of email addresses and want to find all the ones from gmail.com.
The collapse Argument
When you pass a vector to paste(), each element is joined separately. The collapse argument merges all results into a single string.
Common Mistakes
Summary Reference Table
| Function | Purpose | Example | Output |
|---|---|---|---|
| paste() | Join with separator | paste("a","b") | "a b" |
| paste0() | Join without separator | paste0("a","b") | "ab" |
| nchar() | Count characters | nchar("Hello") | [1] 5 |
| substr() | Extract substring | substr("abc",1,2) | "ab" |
| grep() | Find match indices | grep("a",c("a","b")) | [1] 1 |
| grepl() | TRUE/FALSE match | grepl("a",c("a","b")) | [1] TRUE FALSE |
Question 1 of 5
What does paste0("data", "_", "2024") return?
Question 2 of 5
What does nchar("R is fun!") return?
Question 3 of 5
What does substr("Banana", 2, 4) return?
Question 4 of 5
Given x <- c("cat", "catch", "dog", "catfish"), what does grepl("cat", x) return?
Question 5 of 5
What is the difference between paste("X", "Y") and paste("X", "Y", sep="")?