<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/hacking-strings-with-stringi/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/07/28/hacking-strings-with-stringi/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-strings-with-stringi"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/string.png" target="_blank"><img alt="" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/string.png?resize=300%2C168" class="wp-image-3037" data-recalc-dims="1"></a></p>
<p>In the last set of <a rel="nofollow" href="http://www.r-exercises.com/2017/07/14/hacking-strings-with-stringr/" target="_blank">exercises</a>, we worked on the basic concepts of string manipulation with stringr. In this one we will go further into hacking strings universe and learn how to use stringi package.Note that stringi acts as a backend of stringr but have many more useful string manipulation functions compared to stringr and one should really know stringi for text manipulation . </p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/07/28/hacking-strings-with-stringi-solutions/" target="_blank">here</a>.<br>
If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p><strong>Exercise 1</strong><br>
create two strings<br>
<code>c1 &lt;- &quot;a quick brown fox jumps over a lazy dog&quot; </code><br>
<code> c2 &lt;- &quot;a quick brown fox jump over a lazy dog&quot; </code><br>
Now stringi comes with many functions and wrappers around functions to check if two string are equivalent. Check if they are equivalent with<br>
stri_compare, %s&lt;=% and try to reason about the answers. </p>
<div>
<aside><img src="http://www.r-exercises.com/2017/07/28/hacking-strings-with-stringi/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-strings-with-stringi"></aside>
<div><strong>Learn more</strong> about Text analysis in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/text-analyticstext-mining-using-r/" target="_blank">Text Analytics/Text Mining Using R</a>. In this course you will learn how create, analyse and finally visualize your text based data source. Having all the steps easily outlined will be a great reference source for future work.</div>
</div>
<p><strong>Exercise 2</strong></p>
<p>How would you find no of words in c1 and c2 . Its pretty easy with stringi.Find it out . </p>
<p><strong>Exercise 3</strong></p>
<p>Similarly How would you find all words in c1 and c2 . Again its pretty straight forward with stringi.Find it out . </p>
<p><strong>Exercise 4</strong><br>
Lets say you have a vector which contains famous mathematicians<br>
<code> genius &lt;- c(Godel,Hilbert,Cantor,Gauss, Godel, Fermet,Gauss) </code><br>
Find the duplications .</p>
<p><strong>Exercise 5</strong></p>
<p>Find the number of characters in genius vector by stri function. </p>
<p><strong>Exercise 6</strong><br>
Its important to keep the character’s of a set of strings in same encoding .Suppose you have a vector<br>
<code> Genius1 &lt;- c(&quot;Godel&quot;,&quot;Hilbert&quot;,&quot;Cantor&quot;,&quot;Gauss&quot;, &quot;Gödel&quot;, &quot;Fermet&quot;,&quot;Gauss&quot;)</code><br>
Now basically Godel and Gödel are same person but the encoding of the characters are different . but if you try to compare them in a naive way they will act as different .So for the sake of consistency,we should really translate it to similar encoding .Find it how .</p>
<p>Hint – use “Latin-ASCII” transliterator in stri_trans* like function.</p>
<p><strong>Exercise 7</strong><br>
How do we collapse the LETTER vector in R such that it looks like this<br>
“A-B_C-D_E-F_G-H_I-J_K-L_M-N_O-P_Q-R_S-T_U-V_W-X_Y-Z_”</p>
<p><strong>Exercise 8</strong><br>
Suppose you have a string of words like c1 that we have created earlier . You might want to know the starting and end index of the first word, last word.which is obvious for start index of first word and last word but not so obvious for the end index of first word and start index of last word. How would you find this .</p>
<p><strong>Exercise 9</strong><br>
Suppose I have a string<br>
<code> pun &lt;- &quot;A statistician can have his head in an oven and his feet in ice, and he will say that on the average he feels fine&quot; </code><br>
Suppose I want to replace statistician and average with mathematician and median in the string pun .How can I achieve that .<br>
Hint -use a stri_replace* method. </p>
<p><strong>Exercise 10</strong><br>
My string x is like<br>
<code> x &lt;- &quot;I AM SAM. I AM SAM. SAM I AM&quot;</code><br>
replace last SAM with ADAM. </p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/14/hacking-strings-with-stringr/" title="Hacking strings with stringr" target="_blank">Hacking strings with stringr </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/08/05/higher-order-functions-exercises/" title="Higher Order Functions Exercises" target="_blank">Higher Order Functions Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/18/character-functions-advanced/" title="Character Functions (Advanced)" target="_blank">Character Functions (Advanced) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</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="http://www.r-exercises.com/2017/07/28/hacking-strings-with-stringi/?utm_source=rss&utm_medium=rss&utm_campaign=hacking-strings-with-stringi"> R-exercises</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/c2_AvDm-WXU" height="1"></a></div>