<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/lets-get-started-with-dplyr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/01/12/lets-get-started-with-dplyr/?utm_source=rss&utm_medium=rss&utm_campaign=lets-get-started-with-dplyr"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/01/ANIMATE-HULK-TRANSFORMATION-FLASH-ANIMATION-TUTORIAL-1024x576.jpg?x61433"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-3861" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2017/01/ANIMATE-HULK-TRANSFORMATION-FLASH-ANIMATION-TUTORIAL-1024x576-150x150.jpg?resize=150%2C150" data-recalc-dims="1"></a>The dplyr package by Hadley Wickham is a very useful package that provides “A Grammar of Data Manipulation”. It aims to simplify common data manipulation tasks, and provides “verbs”, i.e. functions that correspond to the most common data manipulation tasks. Have fun playing with dplyr in the exercises below!</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/01/12/lets-get-started-with-dplyr-solution/">here</a>.</p>
<p>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>
Install and load the package dplyr package. Given the metadata:</p>
<p>Wt: weight of the subject (kg).</p>
<p>Dose: dose of theophylline administered orally to the subject (mg/kg).</p>
<p>Time: time since drug administration when the sample was drawn (hr).</p>
<p>conc: theophylline concentration in the sample (mg/L).</p>
<p>Copy and paste this code to get df<br>
<code><br>
df=data.frame(Theoph)<br>
library(dplyr)</code></p>
<p><strong>Exercise 2</strong></p>
<p>Use the <code>names()</code> function to get the column names of df.</p>
<p><strong>Exercise 3</strong></p>
<p>Let’s practice using the <code>select()</code> function. This allows you to work with just column names instead of indices.<br>
a) Select only the columns starting from Subject to Dose<br>
b) Only select the <code>Wt</code> and <code>Dose</code> columns now.</p>
<div>
<aside><img src="http://r-exercises.com/2017/01/12/lets-get-started-with-dplyr/?utm_source=rss&utm_medium=rss&utm_campaign=lets-get-started-with-dplyr"></aside>
<div><strong>Learn more</strong> about dplyr in Section 5 <em>Using dplyr on one and multiple Datasets</em> of the online course <a target="_blank" rel="nofollow" href="http://r-exercises.com/product/r-data-pre-processing-data-management-shape-your-data/">R Data Pre-Processing &amp; Data Management – Shape your Data!</a> <span><span><span data-purpose="course-rating-number">Rated 4.6 / 5 (45 ratings)</span></span></span> <span>• </span> <span data-purpose="course-enrollment-numbers"> 473 students enrolled</span></div>
</div>
<p><strong>Exercise 4</strong><br>
Let’s look at the sample with Dose greater than 5 mg/kg. Use the filter <code>command()</code> to return <code>df</code> with Dose>5′</p>
<p><strong>Exercise 5</strong></p>
<p>Great. Now use filter command to return df with Dose>5 and Time greater than the mean Time.</p>
<p><strong>Exercise 6</strong></p>
<p>Now let’s try sorting the data. Use the <code>arrange()</code> function to<br>
1) arrange df by weight (descending)<br>
2) arrange df by weight (ascending)<br>
3) arrange df by weight (ascending) and Time (descending)</p>
<p><strong>Exercise 7</strong></p>
<p>The <code>mutate()</code> command allows you to create a new column using conditions and data derived from other columns. Use <code>mutate()</code> command to create a new column called trend that equals to Time-mean(Time). This will tell you how far each time value is from its mean. Set <code>na.rm=TRUE</code>.</p>
<p><strong>Exercise 8</strong></p>
<p>Given the meta-data<br>
<code></code></p>
<p>76.2 kg Super-middleweight<br>
72.57 kg Middleweight<br>
69.85 kg Light-middleweight<br>
66.68 kg Welterweight</p>
<p>Use the <code>mutate</code> function to classify the weight using the information above. For the purpose of this exercise, considering anything above 76.2 kg to be Super-middleweight and anything below 66.8 to be Welterweight. Anything below 76.2 to be middleweight and anything below 72.57 to be light-middleweight. Store the classifications under weight_cat. Hint: Use ifelse function() with mutate() to achieve this. Store this back into df.</p>
<p><strong>Exercise 9</strong></p>
<p>Use the <code>groupby()</code> command to group df by weight_cat. This allows us to use aggregated functions similar to group by in SQL. Store this in a df called weight_group</p>
<p><strong>Exercise 10</strong></p>
<p>Use the <code>summarize()</code> command on the weight_group created in Question 9 to find the mean Time and sum of Dose received by each weight categories.</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://r-exercises.com/2017/01/12/lets-get-started-with-dplyr/?utm_source=rss&utm_medium=rss&utm_campaign=lets-get-started-with-dplyr"> 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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/74dEWafnseA"></a></div>