<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/pull-the-right-strings-with-stringr-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/10/01/pull-the-right-strings-with-stringr-exercises/"> 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://i1.wp.com/www.r-exercises.com/wp-content/uploads/2018/08/marionettes-801970_1280.jpg?ssl=1" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2018/08/marionettes-801970_1280-300x186.jpg?resize=300%2C186&ssl=1" alt="" width="300" class="wp-image-11922" data-recalc-dims="1" height="186"></a>By providing a set of wrappers to existing functions, the stringr package allows for simple, consistent and efficient manipulations of strings in R. Even though there are some more basic packages that offer strings-related functions, you might find yourself in need for a more complete and straightforward solution for handling strings in R.</p>
<p>With a simple and consistent syntax, stringr provides some very convenient functions around pattern matching, characters manipulation, whitespace handling and more. The full reference of the package can be found <a rel="nofollow" href="https://stringr.tidyverse.org/reference/index.html" target="_blank">here</a>.</p>
<p>Please find below a set of exercises that will help you practice a variety of stringr functions. The focus is on practical operations that data analysts are required to perform on a daily basis. Answers to the exercises are available <a rel="nofollow" href="https://www.r-exercises.com/2018/10/01/pull-the-right-strings-with-stringr-solutions/" target="_blank">here</a>. And, don’t forget to check out our other exercise sets on the stringr package by following the <a rel="nofollow" href="https://www.r-exercises.com/tag/stringr/" target="_blank">stringr tag</a>.</p>
<p>For the following exercises we will use this data:<br>
<code><br>
addresses &lt;- c(&quot;14 Pine Street, Los Angeles&quot;, &quot;152 Redwood Street, Seattle&quot;, &quot;8 Washington Boulevard, New York&quot;)</code></p>
<p>products &lt;-c(“TV “, ” laptop”, “portable charger”, “Wireless Keybord”, ” HeadPhones “)</p>
<p>long_sentences &lt;-stringr::sentences[1:10]</p>
<p>field_names &lt;-c(“order_number”, “order_date”, “customer_email”, “product_title”, “amount”)</p>
<p>employee_skills &lt;-c(“John Bale (Beginner)”, “Rita Murphy (Pro)”, “Chris White (Pro)”, “Sarah Reid (Medium)”)</p>
<p><strong>Exercise 1</strong><br>
Normalize the <code>addresses</code> vector by replacing capitalized letters with lower-case ones.</p>
<p><strong>Exercise 2</strong><br>
Pull only the numeric part of the <code>addresses</code> vector.</p>
<p><strong>Exercise 3</strong><br>
Split the <code>addresses</code> vector into two parts: address and city. The result should be a matrix.</p>
<p><strong>Exercise 4</strong><br>
Now try to split the <code>addresses</code> vector into three parts: house number, street and city. The result should be a matrix.<br>
Hint: use a <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html" target="_blank">regex</a> lookbehind assertion</p>
<p><strong>Exercise 5</strong><br>
In the <code>long_sentences</code> vector, for sentences that start with the letter “T” or end with the letter “s”, show the first or last word respectively. If the sentence both starts with a “T” and ends with an “s”, show both the first and the last words. Remember that the actual last character of a sentence is usually a period.</p>
<div>
<aside><img src="https://www.r-exercises.com/2018/10/01/pull-the-right-strings-with-stringr-exercises/"></aside>
<div><strong>Learn more</strong> about string manipulation with stringr in the online course <a rel="nofollow" href="https://www.r-exercises.com/product/learn-r-by-intensive-practice/" target="_blank">Learn R by Intensive Practice</a>.</div>
</div>
<p><strong>Exercise 6</strong><br>
Show only the first 20 characters of all sentences in the <code>long_sentences</code> vector. To indicate that you removed some characters, use two consecutive periods at the end of each sentence.</p>
<p><strong>Exercise 7</strong><br>
Normalize the <code>products</code> vector by removing all unnecessary whitespaces (both from the start, the end and the middle), and by capitalizing all letters.</p>
<p><strong>Exercise 8</strong><br>
Prepare the <code>field_names</code> for display, by replacing all of the underscore symbols with spaces, and by converting it to the title-case.</p>
<p><strong>Exercise 9</strong><br>
Align all of the <code>field_names</code> to be with equal length, by adding whitespaces to the beginning of the relevant strings.</p>
<p><strong>Exercise 10</strong><br>
In the <code>employee_skills</code> vector, look for employees that are defined as “Pro” or “Medium”. Your output should be a matrix that have the employee name in the first column, and the skill level (without parenthesis) in the second column. Employees that are not qualified should get missing values in both columns.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2018/02/11/bayesian-inference-mcmc-diagnostics-using-coda/" title="Bayesian Inference – MCMC Diagnostics using coda : Exercises" target="_blank">Bayesian Inference – MCMC Diagnostics using coda : Exercises </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2018/02/23/practice-you-ggplot-skills-exercises/" title="Practice Your ggplot Skills: Exercises" target="_blank">Practice Your ggplot Skills: Exercises </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/08/03/parallel-computing-exercises-snow-and-rmpi-part-3/" title="Parallel Computing Exercises: Snow and Rmpi (Part-3)" target="_blank">Parallel Computing Exercises: Snow and Rmpi (Part-3) </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="https://www.r-exercises.com/2018/10/01/pull-the-right-strings-with-stringr-exercises/"> 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/xW42u73WoQI" height="1"></a></div>