Open In App

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

Last Updated : 18 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

  • String: A string is a set of characters or sequences of characters stored in a contiguous memory location.
  • grepl(“[A-Za-z]”, str): This function will check the presence of an alphabetic character in a string.
  • grepl(“[0-9]” , string): This function will check the presence of a numeric value in a string.
  • grepl(“[^A-Za-z0-9 ]”, str) : This function will check the presence of a special character in a string.
  • grepl(“[A-Za-z0-9]”, str): This function will check the presence of alphabetic characters as well as numeric values in a string.
  • grepl(“substring”, string): The grepl() function is commonly used to check for if a specific pattern or a substring or a number is present in a string or not. It returns a logical vector indicating whether it was able to find that substring or not.
  • grepl(“substring”, string, ignore.case = TRUE): This function will check the presence of a substring in a case-sensitive manner.

General steps needed

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

  • Define a string in which we want to search a substring.
  • We use grepl() function to check for the presence of characters or substrings.
  • Return TRUE if we found a character or substring, else return FALSE.
  • Then print the desired result.

Checking for Alphabetic Characters in a string

R




# 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”.

  • Now we apply if condition to check the presence of Alphabetic Characters in a string using grepl(“[A-Za-z]”, str) function.
  • Since alphabets are present in the string “geeksforgeeks” so if condition will return TRUE and print “Alphabets present”.

Checking digits in a string

R




# 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”.

  • Then we apply if condition to check the presence of numeric value from [0-9] is present in the the define string or not.
  • Since “10” is present in the string “geeksforgeeks_10” so if condition will return TRUE and print “geeksforgeeks_10 contains a digit”.

Checking for Special Characters in a string

R




# 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”.

  • Now we apply if condition to check the presence of special Characters in a string using grepl(“[^A-Za-z0-9 ]”, str) function.
  • Since special character is present in the string “@geeksforgeeks” so if condition will return TRUE and print “special character present”.

Checking for Alphanumeric Characters in a string

R




# 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”.

  • Now we apply if condition to check the presence of Alphanumeric Characters in a string using grepl(“[^A-Za-z0-9 ]”, str) function.
  • Since special character i.e. ‘@’ is present in the string “@geeksforgeeks” so if condition will return TRUE and print “special character present”.

Using grepl() to check the presence of substring

R




# 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”.

  • Then we apply the if condition to check the presence of “for” substring in “geeksforgeeks” string.
  • Since “for” is present in “geeksforgeeks” so it will return TRUE and print “for is present in geeksforgeeks” using cat statement.

Case-Insensitive Search

R




# 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”.

  • Then we apply if condition to check the presence of “For” substring in “GeeksForGeeks” string.
  • We have to check in case sensitive manner so we use grepl(“FOR”, str, ignore.case = TRUE) function which will ignore the upper and lower case and check the presence of ‘FOR’ in “GeeksForGeeks”.
  • Since ‘FOR’ is present in the string so, if condition will return TRUE and print “FOR is present in GeeksForGeeks” using cat statement.

Checking for Multiple Substrings

R




# 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!”.

  • Then we apply if condition to 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.
  • Since ‘R’ and ‘is’ both are present in the string so, if condition will return TRUE and print “Either ‘R’ or ‘is’ is present in the string”using cat statement.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads