<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-work-with-strings-in-base-r-an-overview-of-20-methods-for-daily-use/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>In this post in the <a rel="nofollow" href="http://jozefhajnala.gitlab.io/r/categories/rcase4base/" target="_blank">R:case4base</a> series we will look at string manipulation with base R, and provide an overview of a wide range of functions for our string working needs.</p>
<blockquote>
<p>We will use simple examples to learn to perform basic string operations, concatenate strings, work with substrings, switch cases, quote, find and replace within strings and more. Some interesting bonuses will also be included.</p>
</blockquote>
<p>As always, some popular alternatives to base R will also be suggested and many useful references provided for further reading.</p>
</div>
<div>
<h1>Contents</h1>
<ol>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#quick-overview-of-the-very-basics" target="_blank">Quick overview of the very basics</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#string-concatenation" target="_blank">String concatenation</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#string-manipulation-and-properties" target="_blank">String manipulation and properties</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#basic-pattern-matching-and-replacement" target="_blank">Basic pattern matching and replacement</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#bonuses" target="_blank">Bonuses</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#alternatives-to-base-r" target="_blank">Alternatives to base R</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#tldr---just-want-the-code" target="_blank">TL;DR – Just want the code</a></li>
<li><a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#references" target="_blank">References</a></li>
</ol>
</div>
<div>
<h1>Quick overview of the very basics</h1>
<p>This post is aimed to serve as an overview of functionality provided by base R to work with strings. Note that the term “string” is used somewhat loosely and refers to character vectors and character strings. In R documentation, references to <code>character string</code>, refer to character vectors of length 1.</p>
<p>Also since this is an overview, we will not examine the details of the functions, but rather list examples with simple, intuitive explanations trading off technical precision.</p>
<pre><code># String constants can be assigned using
# double quotes 
a &lt;- &quot;this is a character string&quot;
# or single quotes 
b &lt;- 'this is a character string, too'
# To use literal quotes, we can escape with `\`: 
c &lt;- &quot;this is \&quot;it\&quot;&quot;

# To make a character vector with multiple elements:
d &lt;- c(&quot;this&quot;, &quot;vector&quot;, &quot;has&quot;, &quot;five&quot;, &quot;elements&quot;)

# To get the length of a character vector
# (how many elements are in a character vector)
length(d)</code></pre>
<pre><code>## [1] 5</code></pre>
<pre><code># To get the number of characters in elemets of a vector
# (&quot;how many characters in each of the elements&quot;)
nchar(d)</code></pre>
<pre><code>## [1] 4 6 3 4 8</code></pre>
<pre><code># To create a missing character value
NA_character_</code></pre>
<pre><code>## [1] NA</code></pre>
<pre><code># To test if an object is a character vector
is.character(&quot;is this a character vector?&quot;)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<pre><code># To convert other objects to character vectors
# Can surprise the unwary
as.character(c(
  42,
  Sys.time(),
  factor(&quot;A&quot;, levels = LETTERS)
))</code></pre>
<pre><code>## [1] &quot;42&quot;               &quot;1543060557.44415&quot; &quot;1&quot;</code></pre>
<pre><code># One of the ways to output a vector is `cat`
cat(&quot;Show me this&quot;)</code></pre>
<pre><code>## Show me this</code></pre>
<pre><code># To include line breaks use `&quot;\n&quot;`
# To include tabs use `&quot;\t&quot;`:
cat(&quot;Break\ta\ta\nline&quot;)</code></pre>
<pre><code>## Break    a   a
## line</code></pre>
<pre><code># When in doubt about an object
# str or summary may help
weirdList &lt;- list(
  &quot;What is this?&quot;,
  Sys.time(),
  b = 5L,
  c = c(&quot;one&quot;, 2),
  d = factor(c(&quot;red&quot;, &quot;blue&quot;)),
  e = NA_character_,
  f = NA_integer_
)

str(weirdList)</code></pre>
<pre><code>## List of 7
##  $  : chr &quot;What is this?&quot;
##  $  : POSIXct[1:1], format: &quot;2018-11-24 11:55:57&quot;
##  $ b: int 5
##  $ c: chr [1:2] &quot;one&quot; &quot;2&quot;
##  $ d: Factor w/ 2 levels &quot;blue&quot;,&quot;red&quot;: 2 1
##  $ e: chr NA
##  $ f: int NA</code></pre>
<pre><code>summary(weirdList)</code></pre>
<pre><code>##   Length Class   Mode     
##   1      -none-  character
##   1      POSIXct numeric  
## b 1      -none-  numeric  
## c 2      -none-  character
## d 2      factor  numeric  
## e 1      -none-  character
## f 1      -none-  numeric</code></pre>
</div>
<div>
<h1>String concatenation</h1>
<p>String concatenation is the process of “joining” two strings together and one the most common operations.</p>
<div>
<h2>Simple concatenation</h2>
<pre><code># We will use these vectors for our examples:
1:3</code></pre>
<pre><code>## [1] 1 2 3</code></pre>
<pre><code>month.name</code></pre>
<pre><code>##  [1] &quot;January&quot;   &quot;February&quot;  &quot;March&quot;     &quot;April&quot;     &quot;May&quot;      
##  [6] &quot;June&quot;      &quot;July&quot;      &quot;August&quot;    &quot;September&quot; &quot;October&quot;  
## [11] &quot;November&quot;  &quot;December&quot;</code></pre>
<pre><code># Use paste to concatenate
# R recycles 1:3 4 times to fit the length of month.name
paste(1:3, month.name)</code></pre>
<pre><code>##  [1] &quot;1 January&quot;   &quot;2 February&quot;  &quot;3 March&quot;     &quot;1 April&quot;     &quot;2 May&quot;      
##  [6] &quot;3 June&quot;      &quot;1 July&quot;      &quot;2 August&quot;    &quot;3 September&quot; &quot;1 October&quot;  
## [11] &quot;2 November&quot;  &quot;3 December&quot;</code></pre>
<pre><code># Specify the sep argument to 
# separate the elements differently
paste(1:3, month.name, sep = &quot;: &quot;)</code></pre>
<pre><code>##  [1] &quot;1: January&quot;   &quot;2: February&quot;  &quot;3: March&quot;     &quot;1: April&quot;    
##  [5] &quot;2: May&quot;       &quot;3: June&quot;      &quot;1: July&quot;      &quot;2: August&quot;   
##  [9] &quot;3: September&quot; &quot;1: October&quot;   &quot;2: November&quot;  &quot;3: December&quot;</code></pre>
<pre><code># A shorthard for sep = &quot;&quot;
paste0(1:3, month.name)</code></pre>
<pre><code>##  [1] &quot;1January&quot;   &quot;2February&quot;  &quot;3March&quot;     &quot;1April&quot;     &quot;2May&quot;      
##  [6] &quot;3June&quot;      &quot;1July&quot;      &quot;2August&quot;    &quot;3September&quot; &quot;1October&quot;  
## [11] &quot;2November&quot;  &quot;3December&quot;</code></pre>
<pre><code># Alternatively, sprintf is very useful
sprintf(&quot;%s: %s&quot;, 1:3, month.name)</code></pre>
<pre><code>##  [1] &quot;1: January&quot;   &quot;2: February&quot;  &quot;3: March&quot;     &quot;1: April&quot;    
##  [5] &quot;2: May&quot;       &quot;3: June&quot;      &quot;1: July&quot;      &quot;2: August&quot;   
##  [9] &quot;3: September&quot; &quot;1: October&quot;   &quot;2: November&quot;  &quot;3: December&quot;</code></pre>
</div>
<div>
<h2>Concatenate a vector into a single character string</h2>
<pre><code># Provide the collapse argument to paste
# to get a character string (length 1 vector):
paste(1:3, month.name, sep = &quot;: &quot;, collapse = &quot;, &quot;)</code></pre>
<pre><code>## [1] &quot;1: January, 2: February, 3: March, 1: April, 2: May, 3: June, 1: July, 2: August, 3: September, 1: October, 2: November, 3: December&quot;</code></pre>
<pre><code># Or, use toString
toString(paste(1:3, month.name, sep = &quot;: &quot;))</code></pre>
<pre><code>## [1] &quot;1: January, 2: February, 3: March, 1: April, 2: May, 3: June, 1: July, 2: August, 3: September, 1: October, 2: November, 3: December&quot;</code></pre>
</div>
</div>
<div>
<h1>String manipulation and properties</h1>
<div>
<h2>String lengths</h2>
<pre><code># How many elements does a vector have?
length(month.name)</code></pre>
<pre><code>## [1] 12</code></pre>
<pre><code># To get the number of characters in elemets of a vector
# (&quot;how many characters in each of the elements?&quot;)
nchar(month.name)</code></pre>
<pre><code>##  [1] 7 8 5 5 3 4 4 6 9 7 8 8</code></pre>
<pre><code># Are the elements non-empty strings?
nzchar(month.name)</code></pre>
<pre><code>##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE</code></pre>
</div>
<div>
<h2>Switching to upper/lower case</h2>
<pre><code># Switch to all lower case
tolower(month.name)</code></pre>
<pre><code>##  [1] &quot;january&quot;   &quot;february&quot;  &quot;march&quot;     &quot;april&quot;     &quot;may&quot;      
##  [6] &quot;june&quot;      &quot;july&quot;      &quot;august&quot;    &quot;september&quot; &quot;october&quot;  
## [11] &quot;november&quot;  &quot;december&quot;</code></pre>
<pre><code># Switch to all upper case
toupper(month.name)</code></pre>
<pre><code>##  [1] &quot;JANUARY&quot;   &quot;FEBRUARY&quot;  &quot;MARCH&quot;     &quot;APRIL&quot;     &quot;MAY&quot;      
##  [6] &quot;JUNE&quot;      &quot;JULY&quot;      &quot;AUGUST&quot;    &quot;SEPTEMBER&quot; &quot;OCTOBER&quot;  
## [11] &quot;NOVEMBER&quot;  &quot;DECEMBER&quot;</code></pre>
<pre><code># Casefold is a wrapper for S-PLUS compatibility
casefold(month.name, upper = FALSE)</code></pre>
<pre><code>##  [1] &quot;january&quot;   &quot;february&quot;  &quot;march&quot;     &quot;april&quot;     &quot;may&quot;      
##  [6] &quot;june&quot;      &quot;july&quot;      &quot;august&quot;    &quot;september&quot; &quot;october&quot;  
## [11] &quot;november&quot;  &quot;december&quot;</code></pre>
<pre><code>casefold(month.name, upper = TRUE)</code></pre>
<pre><code>##  [1] &quot;JANUARY&quot;   &quot;FEBRUARY&quot;  &quot;MARCH&quot;     &quot;APRIL&quot;     &quot;MAY&quot;      
##  [6] &quot;JUNE&quot;      &quot;JULY&quot;      &quot;AUGUST&quot;    &quot;SEPTEMBER&quot; &quot;OCTOBER&quot;  
## [11] &quot;NOVEMBER&quot;  &quot;DECEMBER&quot;</code></pre>
<pre><code># Also, custom translation:
chartr(&quot;OIZEASGTC&quot;, &quot;01234567(&quot; , toupper(month.name))</code></pre>
<pre><code>##  [1] &quot;J4NU4RY&quot;   &quot;F3BRU4RY&quot;  &quot;M4R(H&quot;     &quot;4PR1L&quot;     &quot;M4Y&quot;      
##  [6] &quot;JUN3&quot;      &quot;JULY&quot;      &quot;4U6U57&quot;    &quot;53P73MB3R&quot; &quot;0(70B3R&quot;  
## [11] &quot;N0V3MB3R&quot;  &quot;D3(3MB3R&quot;</code></pre>
</div>
<div>
<h2>Removing white spaces</h2>
<pre><code># Remove all leading and trailing whitespaces
trimws(&quot; This has trailing spaces.  &quot;)</code></pre>
<pre><code>## [1] &quot;This has trailing spaces.&quot;</code></pre>
<pre><code># Remove leading whitespaces
trimws(&quot; This has trailing spaces.  &quot;, which = &quot;left&quot;)</code></pre>
<pre><code>## [1] &quot;This has trailing spaces.  &quot;</code></pre>
<pre><code># Remove trailing whitespaces
trimws(&quot; This has trailing spaces.  &quot;, which = &quot;right&quot;)</code></pre>
<pre><code>## [1] &quot; This has trailing spaces.&quot;</code></pre>
</div>
<div>
<h2>Encoding conversion</h2>
<pre><code># Convert a character vector between encodings
iconv(&quot;šibrinkuje&quot;, &quot;UTF-8&quot;, &quot;ASCII&quot;, &quot;?&quot;)</code></pre>
<pre><code>## [1] &quot;??ibrinkuje&quot;</code></pre>
</div>
<div>
<h2>Quoting</h2>
<pre><code># Quoting text for fancier priting:
sQuote(month.name)</code></pre>
<pre><code>##  [1] &quot;'January'&quot;   &quot;'February'&quot;  &quot;'March'&quot;     &quot;'April'&quot;     &quot;'May'&quot;      
##  [6] &quot;'June'&quot;      &quot;'July'&quot;      &quot;'August'&quot;    &quot;'September'&quot; &quot;'October'&quot;  
## [11] &quot;'November'&quot;  &quot;'December'&quot;</code></pre>
<pre><code>dQuote(month.name)</code></pre>
<pre><code>##  [1] &quot;\&quot;January\&quot;&quot;   &quot;\&quot;February\&quot;&quot;  &quot;\&quot;March\&quot;&quot;     &quot;\&quot;April\&quot;&quot;    
##  [5] &quot;\&quot;May\&quot;&quot;       &quot;\&quot;June\&quot;&quot;      &quot;\&quot;July\&quot;&quot;      &quot;\&quot;August\&quot;&quot;   
##  [9] &quot;\&quot;September\&quot;&quot; &quot;\&quot;October\&quot;&quot;   &quot;\&quot;November\&quot;&quot;  &quot;\&quot;December\&quot;&quot;</code></pre>
<pre><code># Not to be confused with quoting strings for passing to OS shell
system(paste(&quot;echo&quot;, shQuote(&quot;Weird\nstuff&quot;)))

# Also not be confused with quoting expressions
str(quote(1 + 1))</code></pre>
<pre><code>##  language 1 + 1</code></pre>
</div>
<div>
<h2>Retrieving and working with substrings</h2>
<pre><code># Get the first three characters from all the month.names
substr(month.name, 1, 3)</code></pre>
<pre><code>##  [1] &quot;Jan&quot; &quot;Feb&quot; &quot;Mar&quot; &quot;Apr&quot; &quot;May&quot; &quot;Jun&quot; &quot;Jul&quot; &quot;Aug&quot; &quot;Sep&quot; &quot;Oct&quot; &quot;Nov&quot;
## [12] &quot;Dec&quot;</code></pre>
<pre><code># Get the last three characters from all the month.names
substr(month.name, nchar(month.name) - 2, nchar(month.name))</code></pre>
<pre><code>##  [1] &quot;ary&quot; &quot;ary&quot; &quot;rch&quot; &quot;ril&quot; &quot;May&quot; &quot;une&quot; &quot;uly&quot; &quot;ust&quot; &quot;ber&quot; &quot;ber&quot; &quot;ber&quot;
## [12] &quot;ber&quot;</code></pre>
<pre><code># Wrapper around substr for S Compability:
substring(month.name, 1, 3)</code></pre>
<pre><code>##  [1] &quot;Jan&quot; &quot;Feb&quot; &quot;Mar&quot; &quot;Apr&quot; &quot;May&quot; &quot;Jun&quot; &quot;Jul&quot; &quot;Aug&quot; &quot;Sep&quot; &quot;Oct&quot; &quot;Nov&quot;
## [12] &quot;Dec&quot;</code></pre>
<pre><code># Check whether elements start with a string
startsWith(month.name, &quot;J&quot;)</code></pre>
<pre><code>##  [1]  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
## [12] FALSE</code></pre>
<pre><code># Check whether elements end with a string
endsWith(month.name, &quot;ember&quot;)</code></pre>
<pre><code>##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
## [12]  TRUE</code></pre>
<pre><code># Trim character strings to specified display widths.
strtrim(month.name, 3)</code></pre>
<pre><code>##  [1] &quot;Jan&quot; &quot;Feb&quot; &quot;Mar&quot; &quot;Apr&quot; &quot;May&quot; &quot;Jun&quot; &quot;Jul&quot; &quot;Aug&quot; &quot;Sep&quot; &quot;Oct&quot; &quot;Nov&quot;
## [12] &quot;Dec&quot;</code></pre>
<pre><code># Abbreviate strings to at least minlength characters
abbreviate(month.name, minlength = 3)</code></pre>
<pre><code>##   January  February     March     April       May      June      July 
##     &quot;Jnr&quot;     &quot;Fbr&quot;     &quot;Mrc&quot;     &quot;Apr&quot;     &quot;May&quot;     &quot;Jun&quot;     &quot;Jly&quot; 
##    August September   October  November  December 
##     &quot;Ags&quot;     &quot;Spt&quot;     &quot;Oct&quot;     &quot;Nvm&quot;     &quot;Dcm&quot;</code></pre>
</div>
</div>
<div>
<h1>Basic pattern matching and replacement</h1>
<p>Pattern matching and replacement using regular expressions in an extremely powerful feature, however it is out of scope of this overview to cover them.</p>
<blockquote>
<p>Check the <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/#references" target="_blank">references</a> for better resources if you are interested. A lot more useful detail can also be found in R’s documentation.</p>
</blockquote>
<p>The following is just to show very basic use and list useful functions.</p>
<div>
<h2>Replace substring with other strings</h2>
<pre><code>myStrings &lt;- paste(1:3, month.name, sep = &quot;. &quot;)

# Replace all ones with zeros:
# fixed will match the first argument as is
gsub(&quot;1&quot;, &quot;0&quot;, myStrings, fixed = TRUE)</code></pre>
<pre><code>##  [1] &quot;0. January&quot;   &quot;2. February&quot;  &quot;3. March&quot;     &quot;0. April&quot;    
##  [5] &quot;2. May&quot;       &quot;3. June&quot;      &quot;0. July&quot;      &quot;2. August&quot;   
##  [9] &quot;3. September&quot; &quot;0. October&quot;   &quot;2. November&quot;  &quot;3. December&quot;</code></pre>
<pre><code># Replace only the first &quot;a&quot; in each for &quot;A&quot;
sub(&quot;a&quot;, &quot;A&quot;, myStrings, fixed = TRUE)</code></pre>
<pre><code>##  [1] &quot;1. JAnuary&quot;   &quot;2. FebruAry&quot;  &quot;3. MArch&quot;     &quot;1. April&quot;    
##  [5] &quot;2. MAy&quot;       &quot;3. June&quot;      &quot;1. July&quot;      &quot;2. August&quot;   
##  [9] &quot;3. September&quot; &quot;1. October&quot;   &quot;2. November&quot;  &quot;3. December&quot;</code></pre>
<pre><code># Replace any number with 0
# note that the fixed argument is now FALSE (default)
gsub(&quot;[0-9]&quot;, &quot;0&quot;, myStrings)</code></pre>
<pre><code>##  [1] &quot;0. January&quot;   &quot;0. February&quot;  &quot;0. March&quot;     &quot;0. April&quot;    
##  [5] &quot;0. May&quot;       &quot;0. June&quot;      &quot;0. July&quot;      &quot;0. August&quot;   
##  [9] &quot;0. September&quot; &quot;0. October&quot;   &quot;0. November&quot;  &quot;0. December&quot;</code></pre>
<pre><code># Replace literal dots with 0
gsub(&quot;.&quot;, &quot;0&quot;, myStrings, fixed = TRUE)</code></pre>
<pre><code>##  [1] &quot;10 January&quot;   &quot;20 February&quot;  &quot;30 March&quot;     &quot;10 April&quot;    
##  [5] &quot;20 May&quot;       &quot;30 June&quot;      &quot;10 July&quot;      &quot;20 August&quot;   
##  [9] &quot;30 September&quot; &quot;10 October&quot;   &quot;20 November&quot;  &quot;30 December&quot;</code></pre>
<pre><code># This will replace all characters with zeros
gsub(&quot;.&quot;, &quot;0&quot;, myStrings)</code></pre>
<pre><code>##  [1] &quot;0000000000&quot;   &quot;00000000000&quot;  &quot;00000000&quot;     &quot;00000000&quot;    
##  [5] &quot;000000&quot;       &quot;0000000&quot;      &quot;0000000&quot;      &quot;000000000&quot;   
##  [9] &quot;000000000000&quot; &quot;0000000000&quot;   &quot;00000000000&quot;  &quot;00000000000&quot;</code></pre>
</div>
<div>
<h2>Check if a pattern is present within elements of a character vector</h2>
<pre><code>myStrings &lt;- paste(1:3, month.name, sep = &quot;. &quot;)

# Is a pattern present (returns a logical vector)?
grepl(&quot;ember&quot;, myStrings)</code></pre>
<pre><code>##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
## [12]  TRUE</code></pre>
<pre><code># In which elements is a pattern present (returns indices)?
grep(&quot;ember&quot;, myStrings)</code></pre>
<pre><code>## [1]  9 11 12</code></pre>
<pre><code># In which elements is a pattern present (returns the values)?
grep(&quot;ember&quot;, myStrings, value = TRUE)</code></pre>
<pre><code>## [1] &quot;3. September&quot; &quot;2. November&quot;  &quot;3. December&quot;</code></pre>
</div>
<div>
<h2>Check where the matches are within the elements of a character vector</h2>
<pre><code>myStrings &lt;- paste(1:3, month.name, sep = &quot;. &quot;)

# Where is the first &quot;a&quot; located in each of the elements?
# pattern if not found in that element, returns -1
regexpr(&quot;a&quot;, myStrings)</code></pre>
<pre><code>##  [1]  5  9  5 -1  5 -1 -1 -1 -1 -1 -1 -1
## attr(,&quot;match.length&quot;)
##  [1]  1  1  1 -1  1 -1 -1 -1 -1 -1 -1 -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE</code></pre>
<pre><code># Where are all the &quot;a&quot; located in each of the elements?
# If pattern not found in that element, returns -1
gregexpr(&quot;a&quot;, myStrings)</code></pre>
<pre><code>## [[1]]
## [1] 5 8
## attr(,&quot;match.length&quot;)
## [1] 1 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[2]]
## [1] 9
## attr(,&quot;match.length&quot;)
## [1] 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[3]]
## [1] 5
## attr(,&quot;match.length&quot;)
## [1] 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[4]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[5]]
## [1] 5
## attr(,&quot;match.length&quot;)
## [1] 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[6]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[7]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[8]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[9]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[10]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[11]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE
## 
## [[12]]
## [1] -1
## attr(,&quot;match.length&quot;)
## [1] -1
## attr(,&quot;useBytes&quot;)
## [1] TRUE</code></pre>
<pre><code># Where are all the &quot;a&quot; located in the first element?
gregexpr(&quot;a&quot;, myStrings[1])</code></pre>
<pre><code>## [[1]]
## [1] 5 8
## attr(,&quot;match.length&quot;)
## [1] 1 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE</code></pre>
<pre><code># or also 
gregexpr(&quot;a&quot;, myStrings)[[1]]</code></pre>
<pre><code>## [1] 5 8
## attr(,&quot;match.length&quot;)
## [1] 1 1
## attr(,&quot;useBytes&quot;)
## [1] TRUE</code></pre>
<p>We skip <code>regexec</code> here as parenthesized sub-expressions are very much out of scope of this post.</p>
</div>
</div>
<div>
<h1>Bonuses</h1>
<div>
<img alt="Strings" src="https://i1.wp.com/jozefhajnala.gitlab.io/r/img/r007-01-strings.gif?w=456&ssl=1" data-recalc-dims="1">
<p>Strings</p>
</div>
<pre><code># The Levenshtein distance between strings
adist(c(&quot;lazy&quot;, &quot;lasso&quot;, &quot;lassie&quot;), c(&quot;lazy&quot;, &quot;lazier&quot;, &quot;laser&quot;))</code></pre>
<pre><code>##      [,1] [,2] [,3]
## [1,]    0    3    3
## [2,]    3    4    2
## [3,]    4    3    3</code></pre>
<pre><code># Repeat elements of a character vector a given number of times 
strrep(c(&quot;:)&quot;, &quot;:P &quot;, &quot;;) &quot;), 1:3)</code></pre>
<pre><code>## [1] &quot;:)&quot;        &quot;:P :P &quot;    &quot;;) ;) ;) &quot;</code></pre>
<pre><code># Convert strings to integers of a given base
strtoi(c(&quot;101010&quot;, &quot;11111000101&quot;), base =  2L)</code></pre>
<pre><code>## [1]   42 1989</code></pre>
<pre><code>strtoi(c(&quot;2A&quot;, &quot;7C5&quot;), base = 16L)</code></pre>
<pre><code>## [1]   42 1989</code></pre>
<pre><code># Symbolic Number Coding
cors &lt;- lapply(split(iris, iris$Species), function(x) cor(x[, 1:4]))
lapply(cors, symnum, abbr.colnames = 6)</code></pre>
<pre><code>## $setosa
##              Spl.Ln Spl.Wd Ptl.Ln Ptl.Wd
## Sepal.Length 1                          
## Sepal.Width  ,      1                   
## Petal.Length               1            
## Petal.Width                .      1     
## attr(,&quot;legend&quot;)
## [1] 0 ' ' 0.3 '.' 0.6 ',' 0.8 '+' 0.9 '*' 0.95 'B' 1
## 
## $versicolor
##              Spl.Ln Spl.Wd Ptl.Ln Ptl.Wd
## Sepal.Length 1                          
## Sepal.Width  .      1                   
## Petal.Length ,      .      1            
## Petal.Width  .      ,      ,      1     
## attr(,&quot;legend&quot;)
## [1] 0 ' ' 0.3 '.' 0.6 ',' 0.8 '+' 0.9 '*' 0.95 'B' 1
## 
## $virginica
##              Spl.Ln Spl.Wd Ptl.Ln Ptl.Wd
## Sepal.Length 1                          
## Sepal.Width  .      1                   
## Petal.Length +      .      1            
## Petal.Width         .      .      1     
## attr(,&quot;legend&quot;)
## [1] 0 ' ' 0.3 '.' 0.6 ',' 0.8 '+' 0.9 '*' 0.95 'B' 1</code></pre>
</div>
<div>
<h1>Alternatives to base R</h1>
<div>
<h2>Using the tidyverse’s stringr and glue</h2>
<ul>
<li>
<p><a rel="nofollow" href="https://github.com/tidyverse/stringr" target="_blank">Stringr</a> is built on top of <code>stringi</code>and focuses on the most important and commonly used string manipulation functions whereas <code>stringi</code> provides a comprehensive set covering almost anything you can imagine.</p>
</li>
<li>
<p><a rel="nofollow" href="https://github.com/tidyverse/glue" target="_blank">glue</a> strings to data in R. Small, fast, dependency free interpreted string literals.</p>
</li>
</ul>
</div>
<div>
<h2>Using stringi</h2>
<ul>
<li><a rel="nofollow" href="https://github.com/gagolews/stringi" target="_blank">Stringi</a> is an R package for very fast, correct, consistent, and convenient string/text processing in each locale and any native character encoding.</li>
</ul>
</div>
</div>
<div>
<h1>TL;DR – Just want the code</h1>
<blockquote>
<p>No time for reading? <a rel="nofollow" href="https://jozefhajnala.gitlab.io/r/post/data/r007-string-manipulation.r" target="_blank">Click here to get just the code with commentary</a></p>
</blockquote>
</div>
<div>
<h1>References</h1>
<ul>
<li><a rel="nofollow" href="https://www.rdocumentation.org/packages/base/versions/3.5.1/topics/grep?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Pattern Matching And Replacement</a> R documentation</li>
<li><a rel="nofollow" href="https://www.rdocumentation.org/packages/base/versions/3.5.1/topics/regex?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Regular Expressions As Used In R</a></li>
<li><a rel="nofollow" href="https://bookdown.org/rdpeng/rprogdatascience/regular-expressions.html" target="_blank">Regular Expressions</a> in R Programming for Data Science</li>
<li><a rel="nofollow" href="https://www.youtube.com/watch?v=NvHjYOilOf8" target="_blank">Regular Expressions (video)</a></li>
<li><a rel="nofollow" href="https://www.gastonsanchez.com/r4strings/" target="_blank">Handling Strings with R</a> by Gaston Sanchez</li>
<li><a rel="nofollow" href="https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf" target="_blank">Cheat Sheet</a> for basic regular expressions in R</li>
</ul>
<h2>Did you find the article helpful or interesting? Help others find it by sharing</h2>
<div></div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://jozefhajnala.gitlab.io/r/r007-string-manipulation/"> Jozef's Rblog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/yePIuPW1uto" height="1"></a></div>