This article discusses how to check if a text string in R contains at least one digit, only digits, or no digits at all.

**In R, the easiest way to check if a string contains digits is by using the str_detect() function This function, from the stringr package, detects the presence or absence of a specific pattern in a string, for example, digits. Alternatively, one can use the grepl() function.**

Although both functions obtain the same results, *str_detect*() has **one significant advantage**. Since this function is compatible with *dplyr*, you can use it directly in combination with R pipes (i.e., %>%). In contrast, the *grepl*() function always needs additional function, such as *filter*().

In this article, we explain how to use both functions by using the character vector below as an example. The primary goal is to check whether the 4 elements have one, multiple, or no digits.

`my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")`

Additionally, we will also use this string to show how to count the number of digits in a string.

**Do you know:** How to Count the Number of Words in a String?

**Check if a String Contains At Least One Digit**

Throughout this article, we use regular expressions (i.e., regex) to check if a string matches a specific pattern. Regex can be daunting at first, but once you understand how they work, regular expressions become a powerful instrument in your toolkit as an R programmer.

The first operation where we use regular expressions is in checking if a string contains at least one digit.

**You can check if a combination of characters has one or more digits by using either the grepl() function or the str_detect() function. Both functions search for the presence of digits and return TRUE if it finds at least one. Otherwise, they return FALSE.**

Both functions also use the same number of arguments, namely two:

- The string you want to assess. This can either be a single string, a vector, or a column name.
- The pattern you want to look for. For example digits.

Although the arguments are the same, the order of the arguments is different.

In the *grepl*() function, the pattern comes first and the data second.

grepl(pattern, data)

Whereas the *str_detect*() function expects first the data and then the pattern.

str_detect(data, pattern)

To search for the existence of at least one digit in a string, you can use **the pattern “[0-9]”**.

This regular expression checks if the string contains a digit between 0 and 9, irrespectively of the location. Hence, the digit might be located at the start of the string, in the middle, or at the end.

The example below shows how to use the *grepl*() function and the *str_detect*() function to check for any digit in a string.

```
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
grepl("[0-9]", my_data)
```

```
library(stringr)
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
str_detect(my_data, "[0-9]")
```

As you can see, all elements except the first contain digits. Therefore, the outcome is correct.

**Check if a String Contains Only Digits**

Like checking for at least one digit in a string, you can also use a regular expression to determine whether a combination of characters only contains digits.

**The easiest way to check if a string in R contains only digits is by using the grepl() function or the str_detect() function in combination with the regular expression “^[0-9]+$”. This regular expression represents a pattern that starts with a digit and is followed by other digits until the end of the string.**

We will demystify this regular expression by analyzing each component:

**^**: The caret or circumflex anchor indicates that a match must occur at the**start of a string**.**[0-9]**: This represents all the**digits between 0 and 9**.**+**: The plus quantifier indicates a match of the preceding character (i.e., 0-9)**one or more times**.**$**: The dollar anchor indicates that a match must occur at the**end of a string**.

The examples below show how to use this regex to determine if a string contains only digits.

```
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
grepl("^[0-9]+$", my_data)
```

```
library(stringr)
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
str_detect(my_data, "^[0-9]+$")
```

Only the second element of the character vector contains only digits, therefore the outcome is correct.

**Check if a String Does NOT Contain Digits**

In contrast to the previous examples, you can also check if an R string does not contain digits at all.

**You check if a string doesn’t include digits with the grepl() function or the str_detect() function. Both functions check if a pattern is present and return a TRUE or FALSE. In this case, the pattern could contain all characters except digits.**

You can use two different patterns (or regular expressions) for this task:

- You check if the string contains at least one digit with the “[0-9]”-pattern and then invert the outcome with the !-symbol, or
- You use the “^[^0-9]+$”-pattern to check if the characters of a string are all but digits.

Below we show one example per pattern.

```
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
!grepl("[0-9]", my_data)
```

```
library(stringr)
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
str_detect(my_data, "^[^0-9]+$")
```

Since only the first element of our vector doesn’t contain digits, the outcome of the *grepl*() function and *str_detect*() function is correct.

**Count the Number of Digits in a String**

So far, we’ve focused on checking whether a string does (not) contain digits. Now, we will demonstrate how to **count the number of digits in a string**.

**In R, the easiest way to count the number of digits in a string is by using the str_count() function from the stringr package. This function counts the number of occurrences of a specific pattern, for example, digits. Alternatively, you can use a combination of the functions nchar(), gsub(), and a regular expression.**

First, we focus on the latter option.

The idea behind this method is to first remove all non-digits from the string, and then count the remaining characters (i.e., digits). You replace the non-digits with the *gsub*() function and the regular expression “[^0-9]+”. Next, you count the remaining digits with the *nchar*() function.

For example:

```
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
nchar(gsub("[^0-9]+", "", my_data))
```

As the image demonstrates, the outcome of the R code is correct.

Instead of using two functions, you can use the *str_count*() function from the *stringr* package. **This function counts the number of matches in a string, for example, the number of digits.** To identify the digits, you can use the regular expression “[0-9]”.

See the example below.

```
library(stringr)
my_data <- c("Hello", "123", "It's 10pm", "01JAN1900")
str_count(my_data, "[0-9]")
```

Instead of counting the number of digits in a string, you can use the *str_count*() function also to count the number of a specific word in a string.

**Check if a String is a Number**

Although not completely related to the previous questions, you can also **check if a string is a valid number.**

In R, you check if a string is a valid number with the functions *as.numeric*() and *is.na*().

First, the *as.numeric*() function tries to convert a string into a number (either integers or fractions). However, if it is not possible, it returns a NA.

Next, the *is.na*() function counts the elements that could not be converted into a number (i.e., an NA) into TRUE, and the valid numbers into FALSE.

Finally, the !-symbol switches the TRUEs and FALSEs to show elements that are valid numbers as TRUE and other elements as FALSE.

You can use the *supressWarnings*() function to avoid that R will show warnings.

See the example below.

`my_data <- c("Hello", "123", "3.14", "01.01.1900")`

```
my_data <- c("Hello", "123", "3.14", "01.01.1900")
suppressWarnings({!is.na(as.numeric(my_data))})
```