<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/simple-practice-data-wrangling-the-iris-dataset/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/simple-practice-data-wrangling-iris-dataset/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In last weeks post, I emphasized the importance of <a href="http://sharpsightlabs.com/blog/simple-practice-maps-tidyverse/">practicing R and the Tidyverse with small, simple problems</a>, drilling them until you are competent.</p>
<p>In that post, I <a href="http://sharpsightlabs.com/blog/simple-practice-maps-tidyverse/">gave you a few very small scripts to practice</a> (which I suggest that you <em>memorize</em>).</p>
<p>This week, I want to give you another small example. We’re going to clean up the  dataset.</p>
<p>More specifically, we’re going to:</p>
<ol>
<li> Coerce the  dataset from an old-school data frame into a tibble.</li>
<li> Rename the variables, such that the characters are lower case, and such that “snake case” is applied in place of periods.</li>
</ol>
<p>Like last week, this is a <strong>very</strong> simple example. However, (<a href="http://sharpsightlabs.com/blog/simple-practice-maps-tidyverse/">like I mentioned in the past</a>) this is the sort of small task that you’ll need to be able to execute fluidly if you want to <a href="http://sharpsightlabs.com/blog/shipping-analysis-r-data-visualization/">work on larger projects</a>.</p>
<p>If you <a href="http://sharpsightlabs.com/blog/choose-project-practice-data-science/">want to do large, complex analyses</a>, it really pays to first master techniques on a small scale using much simpler datasets.</p>
<p>Ok, let’s dive in.</p>
<p>First, let’s take a look at the complete block of code.</p>
<pre class="wp-code-highlight">
library(tidyverse)
library(stringr)


#------------------
# CONVERT TO TIBBLE
#------------------
# – the iris dataframe is an old-school dataframe
#   ... this means that by default, it prints out
#   large numbers of records.
# - By converting to a tibble, functions like head()
#   will print out a small number of records by default

df.iris &lt;- as_tibble(iris)


#-----------------
# RENAME VARIABLES
#-----------------
# - here, we're just renaming these variables to be more
#   consistent with common R coding &quot;style&quot;
# - We're changing all characters to lower case
#   and changing variable names to &quot;snake case&quot;

colnames(df.iris) &lt;- df.iris %>%
  colnames() %>%
  str_to_lower() %>%
  str_replace_all(&quot;\\.&quot;,&quot;_&quot;)

# INSPECT

df.iris %>% head()

</pre>
<p>What have we done here? We’ve combined several discrete functions of the Tidyverse together in order to perform a small amount of data wrangling.</p>
<p>Specifically, we’ve turned the  dataset into a tibble, and we’ve renamed the variables to be more consistent with modern R code standards and naming conventions.</p>
<p>This example is quite simple, but useful. This is the sort of small task that you’ll need to be able to do in the context of a large analysis.</p>
<h1>Breaking down the script</h1>
<p>To make this a little clearer, let’s break this down into its component parts. </p>
<p>In the section where we renamed the variables, we only used three core functions:</p>
<ul>
<li> </li>
<li> </li>
<li> </li>
</ul>
<p>Each of these individual pieces are pretty straight forward. </p>
<p>We are using  to retrieve the column names. </p>
<p>Then, <a href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">we pipe the output</a> into the  function  to convert all the characters to lower case. </p>
<p>Next, we use  to replace the periods (“.”) with underscores (“_”). This effectively transforms the variable names to “snake case.” (Keep in mind that  uses regular expressions. You have learned regular expressions, right?) </p>
<p>Finally, using the assignment operator (at the upper, left hand side of the code), we assign the resulting transformed column names to the tibble by using .</p>
<p>I will point out that we have used these functions in a “waterfall” pattern; we have <a href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">combined them by using the the pipe operator</a>, , such that the output of one step becomes the immediate input for the next step. This is a key feature of the Tidyverse. We can combine very simple functions together in new ways to accomplish tasks. This might not seem like a big deal, but it is extremely powerful. The modular nature of the Tidyverse functions, when used with the pipe operator, make the Tidyverse flexible and syntactically powerful, while allowing the code to remain <em>clear</em> and easy to read.</p>
<h1>A test of skill: can you write this fluently?</h1>
<p>The functions that we just used are all critical for doing data science in R. With that in mind, this script is a good test of your skill: can you <a href="http://sharpsightlabs.com/blog/are-you-fluent-r/">write code like this fluently</a>, from memory?</p>
<p>That should be your goal.</p>
<p>To get there, you need to know how the <em>individual functions</em> work. What that means is that you need to study the functions (how they work). But to be able to put them into practice, you need to <em>drill</em> them. So after you understand how they work, drill each individual function until you can write each individual function from memory. Next, you should drill small scripts (like the one in this blog post). You ultimately want to be able to “put the pieces together” quickly and seamlessly in order to solve problems and get things done.</p>
<p>I’ve said it before: if you want a great data science job, you need to be one of the best. If you want to be one of the best, you need to master the toolkit. And to master the toolkit, you need to drill.</p>
<h1>Sign up now, and discover how to rapidly master data science</h1>
<p>To rapidly master data science, you need to practice.</p>
<p>You need to know what to practice, and you need to know <strong>how</strong> to practice.</p>
<p>Sharp Sight is dedicated to teaching you how to master the tools of data science as quickly as possible.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">Sign up now for our email list</a>, and you’ll receive regular tutorials and lessons. You’ll learn:</p>
<ul>
<li>What data science tools you should learn (and what not to learn)</li>
<li>How to practice those tools</li>
<li>How to put those tools together to execute analyses and machine learning projects</li>
<li>… and more </li>
</ul>
<p>If you <a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">sign up for our email list <em>right now</em></a>, you’ll also get access to our “Data Science Crash Course” <u>for free</u>.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/" target="_blank">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://sharpsightlabs.com/blog/simple-practice-data-wrangling-iris-dataset/" target="_blank">Simple practice: data wrangling the iris dataset</a> appeared first on <a rel="nofollow" href="http://sharpsightlabs.com/" target="_blank">SHARP SIGHT LABS</a>.</p>
	<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://sharpsightlabs.com/blog/simple-practice-data-wrangling-iris-dataset/"> r-bloggers – SHARP SIGHT LABS</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/kt4ayqLkqUM" height="1"></a></div>