Counting matches
Another stringr
function that takes a vector of strings and a pattern is str_count()
. str_count()
answers the question "How many times does the pattern occur in each string?". It always returns an integer vector of the same length as that of the input vector.
If you count the occurrences of "pepper"
in your pizzas
, you'll find no occurrences in the first, and one each in the second and third,
pizzas <- c("cheese", "pepperoni",
"sausage and green peppers")
str_count(pizzas, pattern = fixed("pepper"))
Perhaps a little more interesting is to count how many "e"
s occur in each order
str_count(pizzas, pattern = fixed("e"))
You'll use str_count()
to find some names with lots of repeated letters.
This is a part of the course
“String Manipulation with stringr in R”
Exercise instructions
- Count the number of
"a"
in eachgirl_names
, store innumber_as
. - Count the number of
"A"
in eachgirl_names
, store innumber_As
. - Create histograms, use the
hist()
function, ofnumber_as
andnumber_As
. Why isnumber_As
only zero or one? - Add together
number_as
andnumber_As
to gettotal_as
. - Subset
girl_names
to only those names wheretotal_as > 4
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Count occurrences of "a" in girl_names
number_as <- ___
# Count occurrences of "A" in girl_names
number_As <- ___
# Histograms of number_as and number_As
___
___
# Find total "a" + "A"
total_as <- ___
# girl_names with more than 4 a's
___
This exercise is part of the course
String Manipulation with stringr in R
Learn how to pull character strings apart, put them back together and use the stringr package.
Time to meet stringr! You'll start by learning about some stringr functions that are very similar to some base R functions, then how to detect specific patterns in strings, how to split strings apart and how to find and replace parts of strings.
Exercise 1: Introducing stringrExercise 2: Putting strings together with stringrExercise 3: String lengthExercise 4: Extracting substringsExercise 5: Hunting for matchesExercise 6: Detecting matchesExercise 7: Subsetting strings based on matchExercise 8: Counting matchesExercise 9: Splitting stringsExercise 10: Parsing strings into variablesExercise 11: Some simple text statisticsExercise 12: Replacing matches in stringsExercise 13: Replacing to tidy stringsExercise 14: ReviewExercise 15: Final challengesWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.