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(), settingvalue = TRUEwill 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'
This exercise is part of the course
Introduction to Natural Language Processing in R
Exercise instructions
- 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()andgrep()to find out how many times you wrote down the word"favorite".
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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))