Checking character type

Basic types / Character type

char1 <- "A"
is_letter <- grepl("[[:alpha:]]", char1)
is_digit <- grepl("[[:digit:]]", char1)
# is_letter = TRUE
# is_digit = FALSE

# Numeric, but not a digit 0-9
half <- "½"
is_half_number <- grepl("\\p{N}", half,
                        perl = TRUE)
is_half_digit <- grepl("[[:digit:]]", half)
# is_half_number = TRUE
# is_half_digit = FALSE

char2 <- "$"
specials <- c("@", "#", "$", "%", "^", "&", "*")
is_in_list <- char2 %in% specials
# is_in_list = TRUE

cat("is_letter =", is_letter, "\n")
cat("is_digit =", is_digit, "\n")
cat("is_half_number =", is_half_number, "\n")
cat("is_half_digit =", is_half_digit, "\n")
cat("is_in_list =", is_in_list, "\n")