<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/" data-count="horizontal" data-text="Introduction to R for Data Science :: Session 5 [Strings in R]" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.exactness.net/post/145115306595"> The Exactness of Mind</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><br></p>
<p>Welcome to Introduction to R for Data Science Session 5: Structuring Data: String manipulation in R. [<i>Strings in R: {base}, {stringr}, and {stringi} packages. String concatenation, splitting, subsetting, and transformation. On encodings in R.<i>]</i></i></p>
<p>The course is co-organized by <a target="_blank" href="http://www.datascience.rs/">Data Science Serbia</a> and <a target="_blank" href="http://en.startit.rs/">Startit</a>. You will find all course material (R scripts, data sets, SlideShare presentations, readings) on these pages.</p>
<p>Check out the <a target="_blank" href="http://www.exactness.net/post/143651799365/introduction-to-r-for-data-science">Course Overview</a> to acess the learning material presented thus far.</p>
<p><a target="_blank" href="http://www.datascience.rs/kurs-r-za-data-science/">Data Science Serbia Course Pages [in Serbian]</a></p>
<p><a target="_blank" href="http://startit.rs/uvod-u-r-za-data-science/">Startit Course Pages [in Serbian]</a></p>
<h2><strong>Lecturers</strong></h2>
<ul>
<li><a target="_blank" href="https://rs.linkedin.com/in/kovacbranko">dipl. ing Branko Kovač</a>, Data Analyst at <a target="_blank" href="http://cube.rs/">CUBE</a>, Data Science Mentor at <a target="_blank" href="https://www.springboard.com/">Springboard</a>, Data Science Serbia</li>
<li><a target="_blank" href="http://www.exactness.net/">Goran S. Milovanović, Phd</a>, <a target="_blank" href="http://www.diplomacy.edu/">DataScientist@DiploFoundation</a>, Data Science Serbia</li>
</ul>
<h2><strong>Summary of Session 5, 26. may 2016 :: Structuring Data: String manipulation in R.</strong></h2>
<p><em>Structuring Data: String manipulation in R. <span>Strings in R: {base}, {stringr}, and {stringi} packages. String concatenation, splitting, subsetting, and transformation. On encodings in R.</span></em></p>
<ul>
<li><a target="_blank" href="http://www.exactness.net/post/145115306595#slides">Session 5 SlideShare</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145115306595#script">Session 5 R Script</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145115306595#readings">Further Readings</a></li>
<li><a target="_blank" href="http://www.exactness.net/post/145115306595#photos">Session Photos</a></li>
</ul>
<h2><strong>Intro to R for Data Science SlideShare :: Session 5</strong></h2>
<p><iframe height="485" width="595" allowfullscreen src="http://www.slideshare.net/slideshow/embed_code/key/NQvAhyUFaGpIgC"> </iframe>
<div> <strong> <a title="Introduction to R for Data Science :: Session 5 [Data Structuring: Strings in R]" target="_blank" href="http://www.slideshare.net/Krule/introduction-to-r-for-data-science-session-5-data-structuring-strings-in-r">Introduction to R for Data Science :: Session 5 [Data Structuring: Strings in R]</a> </strong> from <strong><a target="_blank" href="http://www.slideshare.net/Krule">Goran S. Milovanovic</a></strong> </div>
<p><br><br>
<h2><strong>R script :: Session 5</strong></h2>
<div>
<div>
<pre><span>########################################################</span>
<span># Introduction to R for Data Science</span>
<span># SESSION 5 :: 26 May, 2016</span>
<span># Data Science Community Serbia + Startit</span>
<span># :: Goran S. Milovanović and Branko Kovač ::</span>
<span>########################################################</span>
 
<span># clear all</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/rm"><span>rm</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/list"><span>list</span></a>=<a target="_blank" href="http://inside-r.org/r-doc/base/ls"><span>ls</span></a><span>(</span><span>)</span><span>)</span>
 
<span># Processing strings in R</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span><a target="_blank" href="http://inside-r.org/packages/cran/stringr"><span>stringr</span></a><span>)</span>
 
<span># strings in R are character vectors</span>
stringA <span>&lt;-</span> <span>&quot;Hello world&quot;</span>
stringB <span>&lt;-</span> <span>&quot;Sun shines!&quot;</span>
stringA
stringB
<a target="_blank" href="http://inside-r.org/r-doc/base/is.character"><span>is.character</span></a><span>(</span>stringA<span>)</span> <span># TRUE</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/as.character"><span>as.character</span></a><span>(</span><span>200</span><span>*</span><span>5</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/as.numeric"><span>as.numeric</span></a><span>(</span><span>&quot;1000&quot;</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/as.double"><span>as.double</span></a><span>(</span><span>&quot;3.14&quot;</span><span>)</span>
 
<span># Using &quot; and '</span>
<span># either:</span>
stringA <span>&lt;-</span> <span>&quot;Hello 'World'&quot;</span>
stringA
<span># or</span>
stringA <span>&lt;-</span> <span>'Hello &quot;World&quot;'</span>
stringA <span># prints: &quot;Hello &quot;World&quot;&quot; - what is this: ?</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>stringA<span>)</span>
<span># try:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span>stringA<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>stringA<span>)</span>
<span># Escaping in R: use , the R escape character</span>
stringA <span>&lt;-</span> <span>'Hello <span>&quot;</span>World<span>&quot;</span>'</span>
stringA
<a target="_blank" href="http://inside-r.org/r-doc/base/print"><span>print</span></a><span>(</span>stringA<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span>stringA<span>)</span>
<span># Escaping escaping</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span><span>&quot;<span>\</span>&quot;</span><span>)</span> <span># nice</span>
 
<span># Length of strings</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>stringA<span>)</span> <span># of course</span>
stringC <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;First&quot;</span><span>,</span> <span>&quot;Second&quot;</span><span>)</span><span>;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>stringC<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/nchar"><span>nchar</span></a><span>(</span>stringA<span>)</span> <span># base function</span>
str_length<span>(</span>stringA<span>)</span> <span># stringr function</span>
 
<span># String Concatenation in R</span>
stringC <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span>stringA<span>,</span> stringB<span>)</span> <span># a character vector of length == 2</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/length"><span>length</span></a><span>(</span>stringC<span>)</span>
stringC <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/paste"><span>paste</span></a><span>(</span>stringA<span>,</span> stringB<span>,</span>
                 sep=<span>&quot;, &quot;</span><span>)</span> <span># length == 1, base function</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span>stringC<span>)</span>
<span># sep w. collapse (paste args)</span>
stringC <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span>stringA<span>,</span> stringB<span>)</span>
stringC <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/paste"><span>paste</span></a><span>(</span>stringC<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot;__&quot;</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span>stringC<span>)</span>
<span># paste0 is paste w. sep=&quot;&quot;, faster than paste(), base function</span>
strA <span>&lt;-</span> <span>&quot;One&quot;</span>
strB <span>&lt;-</span> <span>&quot;Two&quot;</span>
strC <span>&lt;-</span> <span>&quot;Three&quot;</span>
paste0<span>(</span>strA<span>,</span> strB<span>,</span> strC<span>)</span>
<span># the collapse argument is used in paste0 as well </span>
strD <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span>strA<span>,</span>strB<span>,</span>strC<span>)</span>
paste0<span>(</span>strD<span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot;-&quot;</span><span>)</span>
<span># stringr concatenation, also has sep and collapse as args</span>
str_c<span>(</span>strA<span>,</span>strB<span>,</span>strC<span>)</span>
str_c<span>(</span>strA<span>,</span>strB<span>,</span>strC<span>,</span> sep=<span>&quot;...&quot;</span><span>)</span>
str_c<span>(</span>strD<span>,</span><a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot;...&quot;</span><span>)</span>
<span># both paste {base} and str_c {stringr} are vectorized</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/paste"><span>paste</span></a><span>(</span><span>&quot;Prefix-&quot;</span><span>,</span> strD<span>,</span> sep=<span>&quot;-&quot;</span><span>)</span>
str_c<span>(</span><span>&quot;Prefix-&quot;</span><span>,</span> strD<span>,</span> sep=<span>&quot;-&quot;</span><span>)</span>
 
<span># Splitting strings in R</span>
<span># with strsplit {base}</span>
stringA <span>&lt;-</span> <span>&quot;The quick brown fox jumps over the lazy dog&quot;</span><span>;</span>
splitA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>)</span> <span># is.list(splitA) == T</span>
splitA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/unlist"><span>unlist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>)</span><span>)</span>
<span># &quot;The quick brown&quot; from &quot;The quick brown fox jumps over the lazy dog&quot;</span>
splitA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/paste"><span>paste</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/unlist"><span>unlist</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>)</span><span>)</span><span>[</span><span>1</span><span>:</span><span>3</span><span>]</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot; &quot;</span><span>)</span>
<span># or</span>
splitA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/paste"><span>paste</span></a><span>(</span><a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>)</span><span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>[</span><span>1</span><span>:</span><span>3</span><span>]</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot; &quot;</span><span>)</span>
<span># advice: use</span>
splitA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/strsplit"><span>strsplit</span></a><span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>,</span>fixed=T<span>)</span> <span># fixed=T says: match the split argument exactly,</span>
<span># otherwise, split is an regular expression; # default is: fixed = FALSE</span>
<span># string split w. {stringr}</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/is.list"><span>is.list</span></a><span>(</span>str_split<span>(</span>stringA<span>,</span> <span>&quot; &quot;</span><span>)</span><span>)</span>
<span># this is interesting:</span>
str_split<span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>,</span> n=<span>3</span><span>)</span>
<span># &quot;The quick brown&quot; from &quot;The quick brown fox jumps over the lazy dog&quot;</span>
paste0<span>(</span>str_split<span>(</span>stringA<span>,</span><span>&quot; &quot;</span><span>,</span> n=<span>4</span><span>)</span><span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>[</span><span>1</span><span>:</span><span>3</span><span>]</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/nlme/collapse"><span>collapse</span></a>=<span>&quot; &quot;</span><span>)</span>
<span># default: str_split(string, pattern, n = Inf), where pattern is regex</span>
str_split<span>(</span>stringA<span>,</span> boundary<span>(</span><span>&quot;word&quot;</span><span>)</span><span>)</span>
<span># very useful:</span>
stringA1 <span>&lt;-</span> <span>&quot;The quick brown fox jumps over the lazy dog&quot;</span>
str_split<span>(</span>stringA1<span>,</span> boundary<span>(</span><span>&quot;word&quot;</span><span>)</span><span>)</span>
stringA1 <span>&lt;-</span> <span>&quot;Above all, don't lie to yourself. The man who lies to himself and listens to his own lie comes to a point that he cannot distinguish the truth within him, or around him, and so loses all respect for himself and for others. And having no respect he ceases to love.&quot;</span>
str_split<span>(</span>stringA1<span>,</span> boundary<span>(</span><span>&quot;word&quot;</span><span>)</span><span>)</span>
str_split<span>(</span>stringA1<span>,</span> boundary<span>(</span><span>&quot;word&quot;</span><span>,</span> skip_word_none = F<span>)</span><span>)</span> <span># including punctuation and special</span>
str_split<span>(</span>stringA1<span>,</span> boundary<span>(</span><span>&quot;line_break&quot;</span><span>)</span><span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/writeLines"><span>writeLines</span></a><span>(</span>str_split<span>(</span>stringA1<span>,</span> boundary<span>(</span><span>&quot;line_break&quot;</span><span>)</span><span>)</span><span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>)</span>
 
<span># play:</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;1 2 3 4 5 6 7 8 9 10&quot;</span><span>)</span> <span># how about this one?</span>
strA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/as.numeric"><span>as.numeric</span></a><span>(</span>str_split<span>(</span>stringA<span>,</span> <span>&quot; &quot;</span><span>)</span><span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>)</span>
w <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/which"><span>which</span></a><span>(</span>strA %% <span>2</span> <span>==</span> <span>0</span><span>)</span>
strA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/as.character"><span>as.character</span></a><span>(</span>strA<span>)</span>
strA<span>[</span>w<span>]</span> <span>&lt;-</span> <span>'evenNum'</span><span>;</span>
 
<span># Subsetting strings</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;Belgrade&quot;</span><span>,</span> <span>&quot;Zagreb&quot;</span><span>,</span> <span>&quot;Ljubljana&quot;</span><span>)</span> <span># {stringr}</span>
str_sub<span>(</span>stringA<span>,</span> <span>1</span><span>,</span> <span>2</span><span>)</span>
<span># counting backwards</span>
str_sub<span>(</span>stringA<span>,</span> <span>-</span><span>3</span><span>,</span> <span>-</span><span>1</span><span>)</span>
<span># {base}</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringA<span>,</span> <span>1</span><span>,</span> <span>3</span><span>)</span>
<span># play:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringA<span>,</span><a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>)</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>)</span><span>)</span>
<span># nope:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringA<span>,</span> <span>-</span><span>2</span><span>,</span> <span>-</span><span>1</span><span>)</span> <span># {base}</span>
 
<span># Replacing characters in strings</span>
stringB <span>&lt;-</span> stringA <span># just a copy of stringA</span>
str_sub<span>(</span>stringB<span>,</span><span>1</span><span>,</span><span>2</span><span>)</span> <span>&lt;-</span> <span>&quot;00&quot;</span>
stringB
<span># {base}</span>
stringB <span>&lt;-</span> stringA <span># just a copy of stringA</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringB<span>,</span><span>1</span><span>,</span><span>3</span><span>)</span> <span>&lt;-</span> <span>&quot;WowWow&quot;</span> <span># check the result!</span>
stringB
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringB<span>,</span><span>1</span><span>,</span><span>4</span><span>)</span> <span>&lt;-</span> <span>&quot;WoWWow&quot;</span> <span># check the result!</span>
stringB
<a target="_blank" href="http://inside-r.org/r-doc/base/substr"><span>substr</span></a><span>(</span>stringB<span>,</span><span>1</span><span>,</span><span>6</span><span>)</span> <span>&lt;-</span> <span>&quot;WowWow&quot;</span> <span># check the result!</span>
stringB
 
<span># UPPER CASE to lower case and vice versa in R</span>
stringA <span>&lt;-</span> <span>&quot;ABRACADABRA&quot;</span>
<span># {base}</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/tolower"><span>tolower</span></a><span>(</span>stringA<span>)</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/tolower"><span>tolower</span></a><span>(</span>stringA<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/toupper"><span>toupper</span></a><span>(</span>stringA<span>)</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/toupper"><span>toupper</span></a><span>(</span>stringA<span>)</span>
<span># {stringr}</span>
str_to_lower<span>(</span>stringA<span>)</span>
stringB <span>&lt;-</span> str_to_lower<span>(</span>stringA<span>)</span>
str_to_upper<span>(</span>stringA<span>)</span>
<span># capitalize first letter</span>
str_to_title<span>(</span>stringB<span>)</span>
 
<span># Remove whitespace</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot; Remove whitespace &quot;</span><span>)</span><span>;</span>
str_trim<span>(</span>stringA<span>)</span>
<span># remove leading whitespace</span>
str_trim<span>(</span>stringA<span>,</span>side=<span>&quot;left&quot;</span><span>)</span>
<span># remove trailing whitespace</span>
str_trim<span>(</span>stringA<span>,</span>side=<span>&quot;right&quot;</span><span>)</span>
<span># remove all whitespace?</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot; Remove whitespace &quot;</span><span>)</span> <span># how about this one?</span>
<span># there are different ways to do it. Try:</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/gsub"><span>gsub</span></a><span>(</span><span>&quot; &quot;</span><span>,</span> <span>&quot;&quot;</span><span>,</span> stringA<span>,</span> fixed=T<span>)</span> <span># (!(fixed==T)), the first (pattern) argument is regex</span>
<span># in general:</span>
stringA <span>&lt;-</span> <span>&quot;The quick brown fox jumps over the lazy dog The quick brown&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/gsub"><span>gsub</span></a><span>(</span><span>&quot;The quick brown&quot;</span><span>,</span> <span>&quot;The slow red&quot;</span><span>,</span> stringA<span>,</span> fixed=T<span>)</span>
 
<span># Searching for something in a string</span>
<span># Does a string encompass a substring?</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/grepl"><span>grepl</span></a><span>(</span><span>&quot;The quick brown&quot;</span><span>,</span> stringA<span>,</span> fixed = T<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/grepl"><span>grepl</span></a><span>(</span><span>&quot;The fast red&quot;</span><span>,</span> stringA<span>,</span> fixed = T<span>)</span>
stringB <span>&lt;-</span> <span>&quot;Uraaaaaaaa&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/grep"><span>grep</span></a><span>(</span><span>&quot;The quick brown&quot;</span><span>,</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span>stringA<span>,</span> stringB<span>)</span><span>,</span> fixed = T<span>)</span>
<span># where?</span>
stringA <span>&lt;-</span> <span>&quot;The quick brown fox jumps over the lazy dog The quick brown&quot;</span>
w <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/gregexpr"><span>gregexpr</span></a><span>(</span><span>&quot;The quick brown&quot;</span><span>,</span> stringA<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/utils/str"><span>str</span></a><span>(</span>w<span>)</span>
b1 <span>&lt;-</span> w<span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>[</span><span>1</span><span>]</span> <span># first match starts at</span>
b2 <span>&lt;-</span> w<span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>[</span><span>2</span><span>]</span> <span># second match starts at</span>
<span># now, match.length is an attribute of w[[1]], not w itself:</span>
e1 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/attr"><span>attr</span></a><span>(</span>w<span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>,</span><span>&quot;match.length&quot;</span><span>,</span>exact = T<span>)</span><span>[</span><span>1</span><span>]</span>
e2 <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/attr"><span>attr</span></a><span>(</span>w<span>[</span><span>[</span><span>1</span><span>]</span><span>]</span><span>,</span><span>&quot;match.length&quot;</span><span>,</span>exact = T<span>)</span><span>[</span><span>2</span><span>]</span>
<span># first match extraction:</span>
str_sub<span>(</span>stringA<span>,</span>b1<span>,</span>b1<span>+</span>e1<span>-</span><span>1</span><span>)</span>
<span># second match extraction:</span>
str_sub<span>(</span>stringA<span>,</span>b2<span>,</span>b2<span>+</span>e2<span>-</span><span>1</span><span>)</span>
<span># Ok, but easier and more convenient with {stringr}</span>
str_detect<span>(</span>stringA<span>,</span><span>&quot;The quick brown&quot;</span><span>)</span> <span># T or F</span>
str_locate<span>(</span>stringA<span>,</span><span>&quot;The quick brown&quot;</span><span>)</span> <span># first match</span>
str_locate_all<span>(</span>stringA<span>,</span><span>&quot;The quick brown&quot;</span><span>)</span> <span># all matches</span>
<span># term frequency, as we know, is very important in text-mining:</span>
term1 <span>&lt;-</span> str_locate_all<span>(</span>stringA<span>,</span><span>&quot;The quick brown&quot;</span><span>)</span><span>[</span><span>[</span><span>1</span><span>]</span><span>]</span> <span># all matches for term1 ie. &quot;The quick brown&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/dim"><span>dim</span></a><span>(</span>term1<span>)</span><span>[</span><span>1</span><span>]</span> <span># how many matches = how many rows in the str_locate_all output matrix...</span>
 
<span># Sorting strings in R</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;New York&quot;</span><span>,</span> <span>&quot;Paris&quot;</span><span>,</span> <span>&quot;London&quot;</span><span>,</span> <span>&quot;Moscow&quot;</span><span>,</span> <span>&quot;Tokyo&quot;</span><span>)</span>
str_sort<span>(</span>stringA<span>,</span> locale=<span>&quot;en&quot;</span><span>)</span> <span># locale</span>
str_sort<span>(</span>stringA<span>,</span> locale=<span>&quot;haw&quot;</span><span>)</span> <span># locale = Hawaiian</span>
str_sort<span>(</span>stringA<span>)</span>
<span>letters</span>
str_sort<span>(</span><span>letters</span><span>,</span> locale=<span>&quot;en&quot;</span><span>)</span> <span># locale = en</span>
str_sort<span>(</span><span>letters</span><span>,</span> locale=<span>&quot;haw&quot;</span><span>)</span> <span># locale = Hawaiian</span>
<span># backwards</span>
str_sort<span>(</span><span>letters</span><span>,</span>decreasing = T<span>)</span>
<span># handy:</span>
stringA <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/c"><span>c</span></a><span>(</span><span>&quot;New York&quot;</span><span>,</span> <span>&quot;Paris&quot;</span><span>,</span> <span>NA</span><span>,</span> <span>&quot;Moscow&quot;</span><span>,</span> <span>&quot;Tokyo&quot;</span><span>)</span>
str_sort<span>(</span>stringA<span>,</span> na_last=T<span>)</span>
<span># [1] &quot;Moscow&quot; &quot;New York&quot; &quot;Paris&quot; &quot;Tokyo&quot; NA </span>
str_sort<span>(</span>stringA<span>,</span> na_last=F<span>)</span>
<span># [1] NA &quot;Moscow&quot; &quot;New York&quot; &quot;Paris&quot; &quot;Tokyo&quot; </span>
<span># {base}</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/sort"><span>sort</span></a><span>(</span>stringA<span>)</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/sort"><span>sort</span></a><span>(</span>stringA<span>,</span> decreasing=T<span>)</span>
 
<span># Encodings</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/Encoding"><span>Encoding</span></a><span>(</span>stringA<span>)</span>
<span># [1] &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/Encoding"><span>Encoding</span></a><span>(</span>stringA<span>)</span> <span>&lt;-</span> <span>&quot;UTF-8&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/Encoding"><span>Encoding</span></a><span>(</span>stringA<span>)</span>
<span># [1] &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot; &quot;unknown&quot;</span>
<span># Encoding() returns &quot;unknown&quot; if a character string has a &quot;native encoding&quot; mark</span>
<span># or if it is in ASCII</span>
<span># try this!</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/library"><span>library</span></a><span>(</span>stringi<span>)</span>
stri_enc_isutf8<span>(</span>stringA<span>)</span> <span># stringi</span>
<span># [1] TRUE TRUE NA TRUE TRUE</span>
<span># list all suported encodings</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/iconvlist"><span>iconvlist</span></a><span>(</span><span>)</span> <span># {base}</span>
<span># get default encoding</span>
stri_enc_get<span>(</span><span>)</span>
<span># set encoding # COMMENTED CODE with a good reason; DO NOT DO THIS</span>
<span># stri_enc_set(&quot;windows-1253&quot;)</span>
<span># force UTF-8</span>
x <span>&lt;-</span> <span>&quot;Arijadna&quot;</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/Encoding"><span>Encoding</span></a><span>(</span>x<span>)</span>
stri_enc_isutf8<span>(</span>x<span>)</span> <span># {stringi}</span>
x <span>&lt;-</span> <a target="_blank" href="http://inside-r.org/r-doc/base/iconv"><span>iconv</span></a><span>(</span>x<span>,</span>to=<span>'UTF-8'</span><span>,</span><a target="_blank" href="http://inside-r.org/r-doc/base/sub"><span>sub</span></a>=<span>'byte'</span><span>)</span><span>;</span> <span># {byte}</span>
stri_enc_isutf8<span>(</span>x<span>)</span> <span># {stringi}</span>
<a target="_blank" href="http://inside-r.org/r-doc/base/Encoding"><span>Encoding</span></a><span>(</span>x<span>)</span>
stri_enc_detect<span>(</span>x<span>)</span> <span># {stringi}</span>
x <span>&lt;-</span> <span>&quot;Čaršija&quot;</span>
stri_enc_detect<span>(</span>x<span>)</span> <span># {stringi} # a heuristic + statistics;</span>
<span># you need to provide at least a few hundreds bytes of character data:</span>
 
dnevnik <span>&lt;-</span> <span>&quot;On je suzan i plah kao jagnje. Treći stoji desno. On je rumen. O njemu ne mogu ništa da kažem. U tuđini sam, postelju sam pokrio bačvanskim ćilimima. Sigurno ih je kakva Bunjevka tkala prosto joj bilo.
On je veseo i bogat, uvek me razveseli u tuđini, kad ga pogledam nebo zarumeni. Ja joj ne rekoh ni reči. Ja je ne ljubim. Ja ljubim nebo, moja je ljubav blaga, u snu, i neprolazna. Nje nema u životu mome. Njene su oči tamne i tople kao urme.
Kosa joj je rumena kao trava u jesen. Ili se to samo čini, tako čini. Ta kosa me dirnu, te kose mi je žao, te uvele trave. Tu kosu poljubih prvo. Njen glas je
detinjast, uzalud mi ona donosi i čita Bergsona: njena je maramica namirisana, a i da nije, mirisala bi od čistote. Njene su oči mračne i rumene kao krv, one su moja radost&quot;</span>
<span># Excerpt from: Journal of Carnojevic (Dnevnik o Čarnojeviću) (1921)</span>
<span># Miloš Crnjanski, <a target="_blank" href="https://en.wikipedia.org/wiki/Milo%C5%A1_Crnjanski">https://en.wikipedia.org/wiki/Milo%C5%A1_Crnjanski</a></span>
 
stri_enc_detect<span>(</span>x<span>)</span> <span># {stringi} # a heuristic + statistics</span>
stri_enc_mark<span>(</span>x<span>)</span> <span># how does stringi see the encoding</span>
 
<span># Take home message on encodings</span>
<span># 1. Most of the time, you simply need to know the source encoding</span>
<span># 2. All of the time *** convert everything to UTF-8*** - as soon as possible</span>
<span># 3. Most {base}, and all {stringr} and {stringi} functions that process strings in R</span>
<span># will convert their output to UTF-8 automatically</span>
<span># Working inside R only, running an English locale, will never cause you any trouble</span>
<span># However, in Data Science you will probably need to do a lot of web-scraping for a living</span>
<span># - and that's where the fan starts.</span>
<span># God bless iconv() - but don't get to excited, it does not avoid all problems</span>
<span># Next session: Thurday, June 2, 2016 :: Linear Regression w. R</span></pre>
</div>
</div>
<p><a title="Created by Pretty R at inside-R.org" target="_blank" href="http://www.inside-r.org/pretty-r">Created by Pretty R at inside-R.org</a></p>
<p><br></p>
<h2><strong>Readings :: Session 6 [2. June, 2016, @Startit.rs, 19h CET]</strong></h2>
<ul>
<li>
<p><a target="_blank" href="http://www.r-tutor.com/elementary-statistics/simple-linear-regression"><i>R Tutorial</i>, by Chi Yau: <b>Chapter: Simple Linear Regression</b></a>
</p>
</li>
<li>
<p><a target="_blank" href="http://r4ds.had.co.nz/model.html"><i>R for Data Science</i>, Garrett Grolemund &amp; Hadley Wickham, Chapter 19: Model [19.0, 19.1, 19.2]</a></p>
</li>
</ul>
<h2><strong>Session 5 Photos</strong></h2>
<figure data-orig-height="422" data-orig-width="750"><img alt="image" data-orig-height="422" src="http://i1.wp.com/67.media.tumblr.com/028d9440ea0e168cd3fe5de64d701262/tumblr_inline_o7yel96CZW1qa0hyw_540.png?resize=456%2C257" data-recalc-dims="1" data-orig-width="750"></figure>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/" data-counturl="http://www.r-bloggers.com/introduction-to-r-for-data-science-session-5-strings-in-r/" data-count="vertical" data-text="Introduction to R for Data Science :: Session 5 [Strings in R]" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.exactness.net/post/145115306595"> The Exactness of Mind</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/JYCDPnqNop4"></a></div>