LoslegenKostenlos loslegen

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'

Diese Übung ist Teil des Kurses

Introduction to Natural Language Processing in R

Kurs anzeigen

Anleitung zur Übung

  • 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".

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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))
Code bearbeiten und ausführen