<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/data-wrangling-cleansing-regular-expressions-33/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/09/13/data-wrangling-cleansing-regular-expressions-33/"> 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/2017/05/monitor.png" target="_blank"><img alt="" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/monitor.png?resize=275%2C140" class="wp-image-5943" data-recalc-dims="1"></a><br>
Data wrangling is the process of importing, cleaning, and transforming raw data into actionable information for analysis. It is a time-consuming process that is estimated to take about 60-80% of analysts’ time. In this series, we will go through this process. It will be a brief series with the goal of crafting the reader’s skills in data wrangling. This is the fourth part of the series and it aims to cover the cleaning of the data used. In previous parts, we learned how to import, reshape, and transform data. The rest of the series will be dedicated to the data cleansing process. In this post, we will go through the regular expressions, which is a sequence of characters that define a search pattern, mainly<br>
for use in pattern matching with text strings. In particular, we will cover the foundations of regular expression functions.</p>
<p>Before proceeding, it might be helpful to look over the help pages for the <code>grep</code>, <code>sub</code>, <code>gsub</code>, <code>strsplit</code>, .</p>
<p>Moreover, please load the following library.<br>
<code>install.packages(&quot;stringr&quot;)</code><br>
<code>library(stringr)</code></p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/09/13/data-wrangling-cleansing-regular-expressions-33-solutions" target="_blank">here</a>. 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>Exercise 1</p>
<p>Find the cars that are Mercedes-Benz (match the pattern ‘Merc’).<br>
Hint: The names of the cars can be retrieved from the command rownames(mtcars)</p>
<p>Exercise 2</p>
<p>Find the cars that are <strong>not</strong> Mercedes-Benz.</p>
<p>Exercise 3</p>
<p>Find the cars that are Mercedes-Benz (match the pattern ‘Merc’), but with a logical output.</p>
<p>Exercise 4</p>
<p>Find the number of Mercedes-Benz in the data set.</p>
<p>Exercise 5</p>
<p>Replace the first ‘a’ of every car with an ‘e’.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/09/13/data-wrangling-cleansing-regular-expressions-33/"></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>Exercise 6</p>
<p>Replace all ‘a’s of every car with ‘</p>
<p>Exercise 7</p>
<p>Separate the brand from the model. (e.g. “Mazda RX4” -> “Mazda” “RX4”).</p>
<p>Exercise 8</p>
<p>Find the cars that are Mercedes-Benz (use the <code>str_detect</code> function).</p>
<p>Exercise 9</p>
<p>Extract the ‘Merc’ string from the cars that contain it.</p>
<p>Exercise 10</p>
<p>Replace the ‘Merc’ string from the cars that contain it with ‘Mercedes-Benz’.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/30/data-wrangling-cleansing-regular-expressions-23/" title="Data wrangling : Cleansing – Regular expressions (2/3)" target="_blank">Data wrangling : Cleansing – Regular expressions (2/3) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/16/data-wrangling-cleansing-regular-expressions-13/" title="Data wrangling : Cleansing – Regular expressions (1/3)" target="_blank">Data wrangling : Cleansing – Regular expressions (1/3) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/10/30/regular-expressions-part-1/" title="Regular Expressions Exercises – Part 1" target="_blank">Regular Expressions Exercises – Part 1 </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/09/13/data-wrangling-cleansing-regular-expressions-33/"> 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/Stbu1CMqpKI" height="1"></a></div>