0% found this document useful (0 votes)
25 views35 pages

Understanding Strings in R

Uploaded by

fruito779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views35 pages

Understanding Strings in R

Uploaded by

fruito779
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

20CS2058 / BASICS OF DATA

ANALYTICS - R
PROGRAMMING AND TABLEAU

Module 2
Strings with stringr
Packages
• library(tidyverse)
• library(stringr)
String Basics
• create strings with either single quotes or
double quotes
• Example:
string1 <- "This is a string“
string2 <- "To put a 'quote' inside a string,
use single quotes“
string3 <- 'To put a "quote" inside a string,
use single quotes'
Output
Multiple String
• Multiple strings are often stored in a character
vector, which you can create with c():
String Length

• str_length() tells you the number of characters in a


string:
• Example1

• Example2
Combining Strings
Example
• Predict the output of the following program

• OUTPUT:
Question1
• Predict the output of the following program
OUTPUT
• "Good morning Roshan."
String: collapse
Subsetting Strings: str_sub()
• extract parts of a string using str_sub()
• takes start and end arguments that give the
(inclusive) position of the substring:
Change cases
Sort & locale
• En- English & haw - Hawaiian
Basic Matches: str_view()
Anchors
• regular expressions will match any part of a
string.
• It’s often useful to anchor the regular
expression so that it matches from the start or
end of the string.
• ^ to match the start of the string.
• $ to match the end of the string.
Example
Match a complete string, anchor it
with both ^ and $:
Repetition
• The next step up in power involves controlling
how many times a pattern matches:
• ?: 0 or 1
• +: 1 or more
• *: 0 or more
Example
Cntd…
• You can also specify the number of matches
precisely:
• {n}: exactly n
• {n,}: n or more
• {,m}: at most m
• {n,m}: between n and m
Example
Detect Matches
• To determine if a character vector matches a
pattern, use str_detect().
• It returns a logical vector the same length as
the input:
Cntd…
Cntd..
• find all words that don’t contain any vowels:
Filter() in String
• Code

• OUTPUT:
str_count()
Mutate in str_count()
• Code
OUTPUT
Replacing Matches
• str_replace() and str_replace_all() allow you
to replace matches with new strings.
• The simplest use is to replace a pattern with a
fixed string.
• With str_replace_all() you can perform
multiple replacements by supplying a named
vector
Example
Splitting
• str_split() : to split a string up into pieces.
• For example, we could split sentences into
words:
Cntd…
Cntd…
stringi vs stringr
• stringr is built on top of the stringi package.
• stringi has 234 functions and stringr has 42 functions.
• The main difference is the prefix: str_ versus stri_.
• Stringi --- stri_
• Stringr---- str_

You might also like