Extract Numbers from Character String Vector in R
Last Updated :
19 Apr, 2025
In this article, we are going to see how to extract Numbers from Character String Vector in R Programming Language. There are different approaches to extract numbers from character string vectors using some in-built functions. It can be done in the following ways:
- Using the gsub() function
- Using the gregexpr() and regmatches() functions
Method 1: Using gsub() function.
gsub() function in R is used to replace patterns in a string. It can also be employed to extract numbers from a string by defining a pattern to capture the number and returning it.
Syntax:
gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,fixed = FALSE, useBytes = FALSE)
Parameters:
- pattern: string to be matched, supports regular expression
- replacement: string for replacement
- x: string or string vector
- Additional parameters: ignore.case, perl, fixed, and useBytes are used to control how the pattern matching is done.
Steps:
- Define the pattern to match the number. A simple pattern for capturing numbers is " .*?([0-9]+).* ".
- Replace the matched number using gsub().
Example:
R
# Sample data
gfg <- c("7g8ee6ks1", "5f9o1r0", "geeks10")
print(gfg)
# Extracting numbers using gsub()
res = as.numeric(gsub(".*?([0-9]+).*", "\\1", gfg))
print(res)
Output:
[1] "7g8ee6ks1" "5f9o1r0" "geeks10"
[1] 7 5 10
Explanation: gsub() captures the first occurrence of digits in each string and returns the numbers as a numeric vector. In this case, it captures 7, 5, and 10 from the strings "7g8ee6ks1", "5f9o1r0", and "geeks10".
Method 2: Using gregexpr() and regmatches() functions
In this method, we use gregexpr() to identify all the positions of the numbers in the strings, and regmatches() to extract those numbers. This approach is useful when you want to extract multiple numbers from a single string.
gregexpr() function: The gregexpr() function searches for patterns in a string and returns the positions of all matches.
Syntax:
gregexpr(pattern, text, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE)
Parameters:
- text: string, the character vector
pattern
: The regular expression to match.
regmatches() function: This function is used to extract or replace matched sub-strings from match data.
Syntax:
regmatches(x, m, invert = FALSE)
Parameters:
- x: a character vector
- m: an object with match data
- invert: a logical, if TRUE, extract or replace the non-matched substrings.
Example:
R
gfg <- c("7g8ee6ks1", "5f9o1r0", "geeks10")
# Extracting all numbers using gregexpr and regmatches
gfg_numbers <- regmatches(gfg, gregexpr("[[:digit:]]+", gfg))
# Convert extracted numbers to numeric
as.numeric(unlist(gfg_numbers))
Output:
[1] 7 8 6 1 5 9 1 0 10
Similar Reads
Extract First or Last n Characters from String in R In this article, we will know how to extract the last and first char from a given string in the R programming language. For the example purpose, "Geeks for Geeks is Great" is included in our example string. Let's take a look at how we can extract the first and last n characters of this example strin
4 min read
Concatenate Vector of Character Strings in R In this article, we will discuss how to concatenate the strings present in two or more vectors in R Programming Language. Discussed below are various methods of doing so. Method 1: Using paste() paste() function is used to combine strings present in vectors passed to it  an argument. Syntax: paste(
4 min read
Count Number of Characters in String in R In this article, we are going to see how to get the number of characters in a string in R Programming Language. The number of characters in a string refers to the length of the string.Examples:Input: GeeksforgeeksOutput: 13Explanation: Total 13 characters in the given string.Input: Hello worldOutput
2 min read
Extract vector from dataframe in R In this article, we will see how to extract vectors from DataFrame in R Programming Language. The approach is simple, by using $ operator, we can convert dataframe column to a vector. Syntax: dataframe_name$columnname Given below are various examples to implement the same Example 1: R # create vecto
1 min read
How to Convert Character to Numeric in R? In this article, we will discuss how to convert characters to numeric in R Programming Language. We can convert to numeric by using as.numeric() function. Syntax: as.numeric(character) where, character is an character vector Example: R # create a vector with 5 characters data = c('1', '2', '3', '4',
1 min read
Convert Character String to Variable Name in R In this article we will discuss how to convert a character string to the variable name in the R programming language i.e. we are going to assign the character string to the variable as the variable name Method 1: Using assign() function We can assign character string to variable name by using assign
2 min read