Open In App

How to Check if Characters are Present in a String in R.

In this article, we will learn how to check the presence of a character, substring or number in a string using R Programming Language. This can be useful for tasks where we want to filter data, pattern matching or data cleaning. We generally use grepl() function, a versatile tool that is used for checking patterns or characters in strings. We’ll search for both case-sensitive and case-insensitive manner, and below are a few examples in which you can learn how to check if characters are present in a string using R language.

Concepts Related to the Topic

General steps needed

To check the presence of characters in a string in R language, follow the below steps:



Checking for Alphabetic Characters in a string




# Define the string
str <- "geeksforgeeks"
 
# apply if condition to check if there are any alphabetic characters in the string "geeksforgeeks"
if (grepl("[A-Za-z]", str)) {
  #print the statement if alphabets are present in the string.
  cat("Alphabets present\n")
} else {
  #print the statement if alphabets are not present in the given string.
  cat("Alphabets not present\n")
}

Output:

Alphabets present

In this example we define a string “geeksforgeeks”.



Checking digits in a string




# Define the string
str <- "geeksforgeeks_10"
 
# Check if the string contains any digit from (0-9)
if (grepl("[0-9]", str)) {
  cat("'geeksforgeeks_10' contains a digit")
} else {
  cat("'geeksforgeeks_10' does not contain digit")
}

Output:

'geeksforgeeks_10' contains a digit

In this example we define a string “geeksforgeeks_10”.

Checking for Special Characters in a string




# Define the string
str <- "@geeksforgeeks"
 
# apply if condition to check if there are any special characters in the string "@geeksforgeeks".
if (grepl("[^A-Za-z0-9 ]", str)) {
  #return true if alphabets are present in the string.
  #Since "@" is present in string so if condition will be true and then print the statement.
  cat("special characters present\n")
} else {
  #print the statement if special symbols are not present in a string.
  cat("special characters not present\n")
}

Output:

special characters present

In this example we define a string “@geeksforgeeks”.

Checking for Alphanumeric Characters in a string




# Define the string
str <- "geeksforgeeks@10"
 
# apply if condition to check if there are any alphanumeric characters in
#the string "geeksforgeeks@10".# Check if the string contains alphanumeric
#characters
if (grepl("[A-Za-z0-9]", str)) {
  cat("alphanumeric characters present\n")
} else {
  cat("alphanumeric characters not present\n")
}

Output:

alphanumeric characters present

In this example we define a string “@geeksforgeeks”.

Using grepl() to check the presence of substring




# Define the string
str <- "geeksforgeeks"
 
# Check the presence of "for" in the string "geeksforgeeks"
if (grepl("for", str)) {
  cat("'for' is present in geeksforgeeks")
} else {
  cat("'for' is not present in geeksforgeeks")
}

Output:

'for' is present in geeksforgeeks

In this example we define a string as “geeksforgeeks”.

Case-Insensitive Search




# Define the string
str <- "GeeksForGeeks"
 
# Check the presence of "FOR" in the string "GeeksForGeeks"
# it will ignore the upper and lower case and compare the string
if (grepl("FOR", str, ignore.case = TRUE)) {
  #print the statement if condition is true
   
  cat("'FOR' is present in GeeksForGeeks")
} else {
  #print this statement when if condition is false.
  cat("'FOR' is not present in GeeksForGeeks.")
}

Output:

'FOR' is present in GeeksForGeeks

In this example we define a string “GeeksForGeeks”.

Checking for Multiple Substrings




# Define the string
str <- "R programming is fun!"
 
# Check if "R" or "is" is present in the string using | operator which indicate logical OR operation.
# grepl("R|is", str) function will check whether R or is is present in the string or not.
if (grepl("R|is", str)) {
  #if present then print the below statement.
  cat("Either 'R' or 'is' is present in the string")
} else {
  #if condition is false then print the below statement.
  cat("Neither 'R' nor 'is' is present in the string")
}

Output:

Either 'R' or 'is' is present in the string

In this example we define a string “R programming is fun!”.

Conclusion

These are the few examples in which you have learnt how to check the presence of a character in a string using R language.


Article Tags :