OneBite.Dev - Coding blog in a bite size

check if a string contains only letters in R

Code snippet on how to check if a string contains only letters in R

  check.string <- function(x) {
    grepl("^[A-Za-z]+$", x)
  } 

This code checks if a string consists of only letters. The function check.string is defined, with one argument x which is the string to be checked. The grepl function is used to check for the pattern. The pattern is defined as ^[A-Za-z]+$ which means string should only contain English alphabets, either uppercase or lowercase, and should not contain any other characters. If the function returns TRUE, it means that the string contains only letters.

r