Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Created June 28, 2021 20:40
Show Gist options
  • Select an option

  • Save luisDVA/36a763f6d4974105b30cf5f6bf06da6f to your computer and use it in GitHub Desktop.

Select an option

Save luisDVA/36a763f6d4974105b30cf5f6bf06da6f to your computer and use it in GitHub Desktop.

Revisions

  1. luisDVA created this gist Jun 28, 2021.
    38 changes: 38 additions & 0 deletions regex-03_regex-data-cleaningyt.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ##%######################################################%##
    # #
    #### Regex in R - Your Turn ####
    # #
    ##%######################################################%##


    # Match the following regular expressions against the test vector below using `str_detect`.
    ## Can you explain the matches?

    # Regular expressions

    # 1. ^dog
    # 2. ^[a-z]+$
    # 3. \\d

    test_vector <- c("Those dogs are small.","dogs and cats",
    "34","(34)","rat","watchdog","placemat",
    "BABY","2011_April","mice")


    # load packages -----------------------------------------------------------
    library(stringr)

    # create text string ------------------------------------------------------
    test_vector <- c("Those dogs are small.","dogs and cats",
    "34","(34)","rat","watchdog","placemat",
    "BABY","2011_April","mice")

    # evaluate if each element contains a match -------------------------------
    # match the literal string dog
    str_detect(test_vector,"^dog") # second element contains match

    # match lowercase
    str_detect(test_vector,"^[a-z]+$") # elements 5,6,7, and 10 matched

    # match numbers
    str_detect(test_vector,"\\d") # elements 3,4, and 9 matched