Replace the First Match of a Pattern from a String in R Programming – sub() Function Last Updated : 05 Jun, 2020 Comments Improve Suggest changes Like Article Like Report sub function in R Language is used to replace the first match of a pattern in a string. If there is a vector of string elements, then it will replace the first match of the pattern from all elements. Syntax: sub(pattern, replacement, string, ignore.case=TRUE/FALSE) Parameters: pattern: string to be matched replacement: string for replacement string: String or String vector ignore.case: Boolean value for case-sensitive replacement Example 1: Python3 1== # R program to illustrate # the use of sub() function # Create a string x <- "Geeksforgeeks" # Calling sub() function sub("eek", "ood", x) # Calling sub() with case-sensitivity sub("gee", "Boo", x, ignore.case = FALSE) # Calling sub() with case-insensitivity sub("gee", "Boo", x, ignore.case = TRUE) Output: [1] "Goodsforgeeks" [1] "GeeksforBooks" [1] "Booksforgeeks" Example 2: Python3 1== # R program to illustrate # the use of sub() function # Create a string x <- c("Geekforgeek", "Geeksforgeeks", "geeksforGeeks") # Calling sub() function sub("Gee", "boo", x) # Calling sub() with case-insensitivity sub("Gee", "boo", x, ignore.case = TRUE) Output: [1] "bookforgeek" "booksforgeeks" "geeksforbooks" [1] "bookforgeek" "booksforgeeks" "booksforGeeks" Comment More infoAdvertise with us Next Article Replace the First Match of a Pattern from a String in R Programming – sub() Function N nidhi_biet Follow Improve Article Tags : R Language R String-Functions Similar Reads Replace all the matches of a Pattern from a String in R Programming - gsub() Function gsub() function in R Language is used to replace all the matches of a pattern from a string. If the pattern is not found the string will be returned as it is. Syntax: gsub(pattern, replacement, string, ignore.case=TRUE/FALSE) Parameters: pattern: string to be matched replacement: string for replacem 1 min read Seek a Match for the Pattern in the String in R Programming - pmatch() Function pmatch() function in R Language is used to seek match for the pattern passed as argument. It returns the strings that begin with the pattern being searched. Syntax: pmatch(pat, string, nomatch) Parameters: pat: pattern vector to be searched string: vector to be matched nomatch: return when no match 1 min read Matching of patterns in a String in R Programming - agrep() Function agrep() function in R Language is used to search for approximate matches to pattern within each element of the given string. Syntax: agrep(pattern, x, ignore.case=FALSE, value=FALSE)Parameters:pattern: Specified pattern which is going to be matched with given elements of the string. x: Specified st 1 min read Find position of a Matched Pattern in a String in R Programming â grep() Function grep() function in R Language is used to search for matches of a pattern within each element of the given string. Syntax: grep(pattern, x, ignore.case=TRUE/FALSE, value=TRUE/FALSE)Parameters: pattern: Specified pattern which is going to be matched with given elements of the string. x: Specified str 1 min read Get the Last parts of a Data Set in R Programming - tail() Function tail() function in R Language is used to get the last parts of a vector, matrix, table, data frame or function. Syntax: tail(x, n) Parameters: x: specified data types n: number of row need to be printed Example 1: Python3 # R program to illustrate # tail function # Calling the tail() function to # g 1 min read Like