Kom igångKom igång gratis

Practicing syntax with grep

You have just completed an ice-breaker exercise at work and you recorded 10 facts about your boss. You saved these 10 facts into a vector named text. Using regular expressions, you want to summarize your bosses' responses.

A few notes on regular expressions in R:

  • When using grep(), setting value = TRUE will print the text instead of the indices.
  • You can combine patterns such as a digit, "\\d", followed by a period "\\.", with "\\d\\."
  • Spaces can be found using "\\s".
  • You can search for a word by simply using the word as your pattern. pattern = 'word'

Den här övningen är en del av kursen

Introduction to Natural Language Processing in R

Visa kurs

Övningsinstruktioner

  • Using grep(), print the text of the responses that contained a numeric number.
  • Find all items with a number followed by a space. Use a regular expression for the number and the space.
  • Use length() and grep() to find out how many times you wrote down the word "favorite".

Interaktiv övning med praktiskt arbete

Testa den här övningen genom att slutföra den här exempelkoden.

# Print off each item that contained a numeric number
___(pattern = ___, x = text, value = TRUE)

# Find all items with a number followed by a space
___(pattern = ___, x = text)

# How many times did you write down 'favorite'?
length(___(pattern = ___, x = text))
Redigera och kör kod