Strings in R
Strings in R
Introduction
A string is a sequence of characters enclosed within single quotes (' ') or double quotes (" "). Strings are used to store and manipulate textual data such as names, addresses, and messages.
In R, strings belong to the character data type.
Examples:
"Hello"
'R Programming'
"Computer Science"
Creating Strings
Strings can be created using either single or double quotes.
str1 <- "Hello"
str2 <- 'R Programming'
print(str1)
print(str2)
Output
[1] "Hello"
[1] "R Programming"
Character Vectors
Strings can be stored as vectors.
names <- c("John", "Mary", "Alex")
print(names)
Output
[1] "John" "Mary" "Alex"
Determining the Data Type
class()
str <- "Computer"
print(class(str))
Output
[1] "character"
typeof()
print(typeof(str))
Output
[1] "character"
String Functions
1. nchar()
Returns the number of characters in a string.
Syntax
nchar(string)
Example
str <- "Computer"
print(nchar(str))
Output
[1] 8
2. paste()
Concatenates strings with a separator.
Syntax
paste(string1, string2, sep=" ")
Example
first <- "R"
second <- "Programming"
result <- paste(first, second)
print(result)
Output
[1] "R Programming"
Using a Custom Separator
paste("2025", "06", "13", sep="-")
Output
[1] "2025-06-13"
3. paste0()
Concatenates strings without spaces.
Example
paste0("R", "Programming")
Output
[1] "RProgramming"
4. toupper()
Converts characters to uppercase.
str <- "Computer Science"
print(toupper(str))
Output
[1] "COMPUTER SCIENCE"
5. tolower()
Converts characters to lowercase.
str <- "Computer Science"
print(tolower(str))
Output
[1] "computer science"
6. substr()
Extracts a substring.
Syntax
substr(string, start, stop)
Example
str <- "Programming"
print(substr(str, 1, 4))
Output
[1] "Prog"
7. substring()
str <- "Programming"
print(substring(str, 5))
Output
[1] "ramming"
8. strsplit()
Splits a string into smaller strings.
Example
str <- "R Programming Language"
words <- strsplit(str, " ")
print(words)
Output
[[1]]
[1] "R" "Programming" "Language"
9. grep()
Searches for patterns and returns indices.
Example
names <- c("John", "Alex", "Alice", "Mary")
print(grep("^A", names))
Output
[1] 2 3
10. grepl()
Returns TRUE or FALSE.
names <- c("John", "Alex", "Alice", "Mary")
print(grepl("^A", names))
Output
[1] FALSE TRUE TRUE FALSE
11. sub()
Replaces the first occurrence of a pattern.
Example
str <- "banana"
print(sub("a", "A", str))
Output
[1] "bAnana"
12. gsub()
Replaces all occurrences.
str <- "banana"
print(gsub("a", "A", str))
Output
[1] "bAnAnA"
13. sprintf()
Formats strings.
Example
name <- "John"
age <- 20
msg <- sprintf("Name = %s, Age = %d", name, age)
print(msg)
Output
[1] "Name = John, Age = 20"
String Comparison
str1 <- "Apple"
str2 <- "Banana"
print(str1 == str2)
Output
[1] FALSE
Lexicographic Comparison
print("Apple" < "Banana")
Output
[1] TRUE
Converting Strings
Character to Numeric
str <- "100"
num <- as.numeric(str)
print(num)
Output
[1] 100
Numeric to Character
num <- 25
str <- as.character(num)
print(str)
Output
[1] "25"
Escape Sequences
| Sequence | Meaning |
|---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\' | Single quote |
Example
cat("Hello\nWorld")
Output
Hello
World
Common String Operations
Reverse a String
str <- "HELLO"
chars <- strsplit(str, "")[[1]]
rev_str <- paste(rev(chars), collapse="")
print(rev_str)
Output
[1] "OLLEH"
Count Vowels
str <- "Computer Science"
chars <- strsplit(tolower(str), "")[[1]]
count <- 0
for(ch in chars)
{
if(ch %in% c("a","e","i","o","u"))
count <- count + 1
}
print(count)
Output
[1] 6
Check Palindrome
str <- "madam"
chars <- strsplit(str, "")[[1]]
rev_str <- paste(rev(chars), collapse="")
if(str == rev_str)
print("Palindrome")
else
print("Not Palindrome")
Output
[1] "Palindrome"
Another Method
str <- readline("Enter a string:") rstr="" for ( i in nchar(str):1) { rstr=paste0(rstr,substr(str,i,i)) } if (str==rstr) cat ("pal\n") else cat ("not pal\n")
Useful String Functions Summary
| Function | Purpose |
|---|---|
nchar() | Length of string |
paste() | Concatenate strings |
paste0() | Concatenate without spaces |
toupper() | Convert to uppercase |
tolower() | Convert to lowercase |
substr() | Extract substring |
substring() | Extract substring |
strsplit() | Split string |
grep() | Pattern matching |
grepl() | Returns TRUE/FALSE |
sub() | Replace first occurrence |
gsub() | Replace all occurrences |
sprintf() | Format strings |
Applications of Strings
- Processing names and addresses.
- Pattern matching and searching.
- Text formatting.
- Parsing files and documents.
- Natural language processing.
- Data cleaning and preprocessing.
Conclusion
Strings are fundamental data structures used to store and manipulate textual information in R. R provides a rich set of built-in functions for string creation, concatenation, searching, extraction, replacement, and formatting. Mastery of string operations is essential for data preprocessing, text analytics, and general-purpose programming.
Comments
Post a Comment