Skip to Content
RStudio for R Statistical Computing Cookbook
book

RStudio for R Statistical Computing Cookbook

by Andrea Cirillo
April 2016
Beginner to intermediate content levelBeginner to intermediate
246 pages
5h 20m
English
Packt Publishing
Content preview from RStudio for R Statistical Computing Cookbook

Dealing with regular expressions

Regular expressions let you easily perform string manipulation with the help of just a handful of characters. In this recipe, we will look for digits, punctuation, and uppercase and lowercase in a vector of strings.

How to do it...

  1. Define a test vector that you can search with regular expressions:
    test_string <- c("012","345",";.","kdj","KSR" ,"\n")
    
  2. Look for digits:
    grep("[[:digit:]]",test_string, value = TRUE)
    which will result in :
    [1] "012" "345"
    
  3. Look for punctuation:
    grep("[[:punct:]]",test_string, value = TRUE)
    which will have as a result
    [1] ";."
    
  4. Look for lowercase letters:
    grep("[[:lower:]]",test_string, value = TRUE)
    

    Which will select the following:

    [1] "kdj"
    
  5. Look for uppercase letters:
     grep("[[:upper:]]",test_string, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Data Analysis Using Jupyter Notebook

Practical Data Analysis Using Jupyter Notebook

Marc Wintjen

Publisher Resources

ISBN: 9781784391034Supplemental Content