String Manipulation in detail
In this article, we will explore different ways to find substrings in the R programming language.
R provides multiple methods for substring operations, including:
- Using
substr()function - Using
str_detect()function - Using
grep()function
Method 1: Using substr() function
The substr() function in R is used to extract a substring from a given string based on specified start and end positions.
Syntax:
substr(string_name, start, end)
Return Value: This function returns the substring from the given string according to the specified start and end indexes.
Example 1:
Operations on a Single Matrix
R allows element-wise operations on matrices using overloaded arithmetic operators. Let’s look at some examples.
# Given String
text <- "Learning R Programming"
# Using substr() function
result <- substr(text, 1, 8)
print(result)
Example:
# Given String
sentence <- "Data science and machine learning"
# Using substr() function
result <- substr(sentence, 13, 27)
print(result)
Output:
[1] "and machine le"
Method 2: Using str_detect() function
The str_detect() function from the stringr package checks whether a specified substring exists within a string. It returns TRUE if a match is found and FALSE otherwise.
Syntax:
str <- paste(c(2:4), "5", sep = "-")
print(str)
Parameters:
string: The target string or vector of strings.pattern: The substring pattern to be searched.
Example:
# Load library
library(stringr)
# Creating a vector
words <- c("Apple", "Banana", "Cherry", "Mango")
# Pattern to search
pattern <- "Banana"
# Using str_detect() function
str_detect(words, pattern)
Output:
[1] FALSE TRUE FALSE FALSE
Method 3: Using grep() function
The grep() function returns the indices of elements in a character vector that match a specified pattern. If multiple matches are found, it returns a list of their respective indices.
Syntax:
grep(pattern, string, ignore.case=FALSE)
Parameters:
pattern: A regular expression pattern to match.string: The character vector to search.ignore.case: Whether to perform case-insensitive searching (default isFALSE).
Example:
# Define vector
text_values <- c("Orange", "orchestra", "blue", "ocean")
# Using grep() to find pattern "or"
matching_indices <- grep("or", text_values, ignore.case=TRUE)
print(matching_indices)
Output:
[1] 1 2 4
Leave a Reply