The Wayback Machine - https://web.archive.org/web/20241127005635/https://www.geeksforgeeks.org/extracting-substrings-from-a-character-vector-in-r-programming-substring-function/
Open In App

Extracting Substrings from a Character Vector in R Programming – substring() Function

Last Updated : 27 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

substring() function in R Programming Language is used to extract substrings in a character vector. You can easily extract the required substring or character from the given string.

Syntax: substring(text, first, last)

Parameters: 

  • text: character vector
  • first: integer, the first element to be replaced
  • last: integer, the last element to be replaced

R – substring() Function Example

Example 1: Extracting values with substring function in R Programming language

R




# R program to illustrate
# substring function
 
# Calling substring() function
substring("Geeks", 2, 3)
substring("Geeks", 1, 4)
substring("GFG", 1, 1)
substring("gfg", 3, 3)


Output : 

[1] "ee"
[1] "Geek"
[1] "G"
[1] "g"

Example 2: Extracting values with substring in string function in R Programming language

R




# R program to illustrate
# substring function
 
# Initializing a string vector
x < - c("GFG", "gfg", "Geeks")
 
# Calling substring() function
substring(x, 2, 3)
substring(x, 1, 3)
substring(x, 2, 2)


Output: 

[1] "FG" "fg" "ee"
[1] "GFG" "gfg" "Gee"
[1] "F" "f" "e"

Example 3: String replacement in R using substring() function

R




# R program to illustrate
# substring function
 
# Initializing a string vector
x <- c("GFG", "gfg", "Geeks")
 
# Calling substring() function
substring(x, 2, 3) <- c("@")
print(x)


Output:

[1] "G@G"   "g@g"   "G@eks"


Previous Article
Next Article

Similar Reads

Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x) Output: [, 1] [, 2] [1, ] 2 7 [2, ] 3 2 [3, ] 4
1 min read
Check for the Existence of a Vector Object in R Programming - is.vector() Function
is.vector() function in R Language is used to return TRUE if the given vector is of the specified mode having no attributes other than names. It returns FALSE otherwise. Syntax: is.vector(x, mode = "any") Parameters: x: R object mode: character string naming an atomic mode or "list" or "expression" or "any" Example 1: # R program to illustrate # is
1 min read
Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
Check if Object is of the Character Data type in R Programming - is.character() Function
is.character() Function in R Language is used to check if the object is of the form of a string/character or not. It will return true if any element of the object is of the character data type. Syntax: is.character(Object) Parameter: Object: It could be an array, list, vector, etc. Example 1: # R Program to illustrate # the use of is.character func
1 min read
Check if an Object is of Type Character in R Programming - is.character() Function
is.character() function in R Language is used to check if the object passed to it as argument is of character type. Syntax: is.character(x) Parameters: x: Object to be checked Example 1: # R program to check if # object is a character # Creating a vector x1 <- 4 x2 <- c("a", "b", "c", "d") x3 <- c(1,
1 min read
Convert a Numeric Object to Character in R Programming - as.character() Function
as.character() function in R Language is used to convert a numeric object to character object. Syntax: as.character(x) Parameters: x: Numeric Object Example 1: # R program to convert a numeric object # to character object # Calling as.character() function as.character(1) as.character(2 + 3) as.character(1.5) Output: [1] "1" [1] "5" [1] "1.5" Exampl
1 min read
Extracting Unique Numbers from String in R
When working with text data in R, you may encounter situations where you need to extract unique numbers embedded within strings. This is particularly useful in data cleaning, preprocessing, or parsing text data containing numerical values. This article provides a theoretical overview and practical examples of extracting unique numbers from a string
4 min read
Extracting a String Between Two Other Strings in R
String manipulation is a fundamental aspect of data processing in R. Whether you're cleaning data, extracting specific pieces of information, or performing complex text analysis, the ability to efficiently work with strings is crucial. One common task in string manipulation is extracting a substring that lies between two other substrings. In this a
3 min read
Find Location and Character Vector of an Object with partial name in R Language - apropos() and find() Function
In this article, we are going to find the location and character vector of an object with a partial name in R Programming Language. R - apropos() Function appros() function in R Language will give the character vector of name that contains the partial name. Syntax: apropos("x", ignore.case, simple.word) Parameters: x: partial-name of objectignore.c
2 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
paste0() and sprintf() functions in R Language can also be used to add leading zeros to each element of a vector passed to it as argument. Syntax: paste0("0", vec) or sprintf("%0d", vec)Parameters: paste0: It will add zeros to vector sprintf: To format a vector(adding zeros) vec: Original vector dataReturns: Vectors by addition of leading zeros Exa
1 min read
Check if Elements of a Vector are non-empty Strings in R Programming - nzchar() Function
nzchar() function in R Language is used to test whether elements of a character vector are non-empty strings. Syntax: nzchar(x) Parameters: x: character vector Example 1: # R program to illustrate # nzchar function # Initializing a character vector x <- c("GFG", "gfg") y <- c("a", "b", "c") #
1 min read
Make Elements of a Vector Unique in R Programming - make.unique() Function
make.unique() function in R Language is used to return elements of a vector with unique names by appending sequence numbers to duplicates. Syntax: make.unique(names, sep)Parameters: names: Character vector with duplicate names sep: Separator to be used Example 1: C/C++ Code # R program to make unique vectors # Calling make.unique() Function make.un
1 min read
Compute the gamma value of a Non-negative Numeric Vector in R Programming - gamma() Function
gamma() function in R Language is used to compute the gamma value of a numeric vector using the gamma function. Gamma function is basically, gamma(x) = factorial(x - 1) Syntax: gamma(x) Parameters: x: Non-negative Numeric Vector Example 1: # R program to calculate the gamma value # Calling gamma Function gamma(2) gamma(3) gamma(5) Output: [1] 1 [1]
1 min read
Check if values in a vector are True or not in R Programming - all() and any() Function
In this article, we are going to check if the values in a vector are true or not in R Programming Language. R - all() function all() function in R Language will check in a vector whether all the values are true or not. Syntax: all(x, na.rm) Parameters: x: vectorna.rm: logical, if NA value to removed before result Example 1: Basic example of all() f
2 min read
Getting Match of an Element within a Vector in R Programming - charmatch() Function
charmatch() function in R Programming Language is used to find matches between two arguments. Syntax: charmatch(x, table, nomatch = NA_integer_) Parameters: x: the values to be matchedtable: the values to be matched againstnomatch: the integer value to be returned at non-matching positionr - charmatch() Function Example Example 1: C/C++ Code # R pr
1 min read
Replace the Elements of a Vector in R Programming - replace() Function
replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. Syntax: replace(x, list, values) Parameters: x: vector list: indices values: replacement values Example 1: # R program to illustrate # replace function # Initializing a string vector x <- c("GF
1 min read
Find String Matches in a Vector or Matrix in R Programming - str_detect() Function
str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string. It will return TRUE for a match found otherwise FALSE against each of the element of the Vector or matrix. Note: This function uses 'stringr' Library. Syntax: str_detect(string, pattern) Parameter: string: specified string pat
1 min read
Convert a Vector into Factor in R Programming - as.factor() Function
as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in R C/C++ Code # Creating a vector x<-c("female", "male", "male", "
1 min read
Check if the elements of a Vector are Finite, Infinite or NaN values in R Programming - is.finite(), is.infinite() and is.nan() Function
is.finite() function in R Language is used to check if the elements of a vector are Finite values or not. It returns a boolean value for all the elements of the vector. Syntax: is.finite(x) Parameters: x: Vector to be checked Example: # R program to illustrate # the use of is.finite() function # Creating a vector x <- c(1, 2, 3, 4, 5, NA, 6, 7)
2 min read
Get the Minimum and Maximum element of a Vector in R Programming - range() Function
range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument. Syntax: range(x, na.rm, finite) Parameters: x: Numeric Vectorna.rm: Boolean value to remove NAfinite: Boolean value to exclude non-finite elementsR - range() Function ExamplesExample 1: Get the Minimum and Maximum elem
1 min read
Calculate the difference between Consecutive pair of Elements of a Vector in R Programming - diff() Function
diff() function in R Language is used to find the difference between each consecutive pair of elements of a vector. Syntax: diff(x, lag, differences) Parameters: x: vector or matrix lag: period between elements differences: Order of difference Example 1: # R program to find the difference # between each pair of elements of a vector # Creating a vec
2 min read
Calculate Median of elements of a Vector in R Programming - median() Function
median() function in R Language is used to calculate the median of the elements of the numeric vector passed as argument. Syntax: median(x, na.rm) Parameters: x: Numeric Vector na.rm: Boolean value to ignore NA Example 1: # R program to calculate median of a vector # Creating vector x1 <- c(2, 3, 5, 7, 4, 8, 6) x2 <- c(-3, -5, 6, 2, -4) # Cal
1 min read
Calculate Rank of the Values of a Vector in R Programming - rank() Function
rank() function in R Language is used to return the sample ranks of the values of a vector. Equal values and missing values are handled in multiple ways. Syntax: rank(x, na.last) Parameters: x: numeric, complex, character, and logical vector na.last: Boolean Value to remove NAs Example 1: # R program to print rank of a vector values # Creating a ve
1 min read
Combine Arguments into a Vector in R Programming - c() Function
c() function in R Language is used to combine the arguments passed to it. Syntax: c(...) Parameters: ...: arguments to be combined Example 1: # R program to cumulative maxima # Calling c() function x <- c(1, 2, 3, 4) x Output: [1] 1 2 3 4 Example 2: # R program to cumulative maxima # Calling c() function x <- c("a", "b",
1 min read
Calculate the Cumulative Minima of a Vector in R Programming - cummin() Function
cummin() function in R Language is used to calculate the cumulative minima of the values of vector passed as arguments. Syntax: cummin(x) Parameters: x: numeric object Example 1: # R program to cumulative minima # Calling cummin() function cummin(2:6) cummin(-2:-6) cummin(1.8:4.2) Output: [1] 2 2 2 2 2 [1] -2 -3 -4 -5 -6 [1] 1.8 1.8 1.8 Example 2:
1 min read
Calculate the Cumulative Maxima of a Vector in R Programming - cummax() Function
The cumulative maxima is the max value of elements 1 through l for an element l of the given variables. cummax() function in R Language is used to calculate the cumulative maxima of the values of vector passed as arguments. Syntax: cummax(x) Parameters: x: numeric object Example 1: C/C++ Code # R program to cumulative maxima # Calling cummax() func
2 min read
Return the Index of the First Minimum Value of a Numeric Vector in R Programming - which.min() Function
which.min() function in R Language is used to return the location of the first minimum value in the Numeric Vector. Syntax: which.min(x) Parameters: x: Numeric Vector Example 1: # R program to find index of # first minimum value # Creating a vector x <- c(2, 3, 4, 5, 1, 2, 3, 1, 2) # Calling which.min() function which.min(x) # Printing minimum v
1 min read
Return the Index of the First Maximum Value of a Numeric Vector in R Programming - which.max() Function
which.max() function in R Language is used to return the location of the first maximum value in the Numeric Vector. Syntax: which.max(x) Parameters: x: Numeric Vector Example 1: # R program to find index of # first maximum value # Creating a vector x <- c(2, 3, 4, 5, 1, 2, 3, 1, 2) # Calling which.max() function which.max(x) # Printing maximum v
1 min read
Convert values of an Object to Logical Vector in R Programming - as.logical() Function
as.logical() function in R Language is used to convert an object to a logical vector. Syntax: as.logical(x)Parameters: x: Numeric or character object R - as.logical() Function ExampleExample 1: Basic example of as.logical() Function in R Programming Language. C/C++ Code # R Program to convert # an object to logical vector # Creating a vector x <
1 min read
three90RightbarBannerImg