Felix Schönbrodt

PD Dr. Dipl.-Psych.

July 2012

Validating email adresses in R

I currently program an automated report generation in R – participants fill out a questionnaire, and they receive a nicely formatted pdf with their personality profile. I use knitr, LaTex, and the sendmailR package.

Some participants did not provide valid email addresses, which caused the sendmail function to crash. Therefore I wanted some validation of email addresses – here’s the function:

isValidEmail <- function(x) {
    grepl("\\<[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\>", as.character(x), ignore.case=TRUE)

Let’s test some valid and invalid adresses:

# Valid adresses
isValidEmail("felix@nicebread.de  ")
isValidEmail("    felix@nicebread.de")

# invalid addresses

The regexp is taken from www.regular-expressions.info and adapted to the R style of regexp. Please note the many comments (e.g., here or here) about “Is there a single regexp that matches all valid email adresses?” (the answer is no).

Comments (7) | Trackback
  • Categories

  • Disclaimer: This is a multi-language site.

    This website is a conglomerate of old and new texts - some are in German, some in English. I do not have the time to translate each, so unfortunately this is a mixed-language site. I'm sorry. If you have any questions about the German texts, just write an email! Diese Webseite ist eine Mischung aus deutschen und englischen Texten - leider habe ich nicht die Zeit sie jeweils in die andere Sprache zu ├╝bersetzen. Falls Sie Fragen zu einem englischen Text haben k├Ânnen Sie mir gerne eine Email schreiben!