© Thomas Mailund 2019
Thomas MailundR Data Science Quick Referencehttps://doi.org/10.1007/978-1-4842-4894-2_8

8. Working with Strings: stringr

Thomas Mailund1 
(1)
Aarhus, Denmark
 
The stringr package gives you functions for string manipulation. The package will be loaded when you load the tidyverse package :
library(tidyverse)
You can also load the package alone using
library(stringr)

Counting String Patterns

The str_count() function counts how many tokens a string contain, where tokens, for example, can be characters or words.

By default, str_count() will count the number of characters in a string.
strings <- c(
    "Give me an ice cream",
    "Get yourself an ice cream",
    "We are all out of ice creams",
    "I scream, you scream, everybody loves ice cream.", ...

Get R Data Science Quick Reference: A Pocket Guide to APIs, Libraries, and Packages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.