<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2016/11/Occasional-Table-Group.jpg" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2016/11/Occasional-Table-Group-300x168.jpg?resize=300%2C168" alt="" class="wp-image-3037" data-recalc-dims="1"></a><br>
In the last set of <a rel="nofollow" href="http://www.r-exercises.com/2017/06/15/data-manipulation-with-data-table-part-1/" target="_blank">exercise</a> of data.table ,we saw some interesting features of data.table .In this set we will cover some of the advanced features like set operation ,join in data.table.You should ideally complete the first part before attempting this one .<br>
Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/06/29/data-manipulation-with-data-table-solution-part-2" target="_blank">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>
Create a data.table from diamonds dataset ,create key using setkey over cut and color .Now select first entry of the groups Ideal and Premium </p>
<p><strong>Exercise 2</strong><br>
With the same dataset,select the first and last entry of the groups Ideal and Premium </p>
<p><strong>Exercise 3</strong><br>
Earlier we have seen how we can create/update columns by reference using <code>:= </code>.However there is a lower over head ,faster alternative in data.table . This is achieved by SET and Loop in data.table ,however this is meant for simple operations and will not work in grouped operation .Now take the diamonds data.table and make columns x,y,z value squared .For example if the value is currently 10 ,the resulting value would be 100 .You are awesome if you find out all alternative answer and check the time using system.time .</p>
<p><strong>Exercise 4</strong><br>
In the same dataset ,capitalize first letter of column names .</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/29/data-manipulation-with-data-table-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=data-manipulation-with-data-table-part-2"></aside>
<div><strong>Learn more</strong> about the data.table package in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/r-data-pre-processing-data-management-shape-your-data/" target="_blank">R Data Pre-Processing &amp; Data Management – Shape your Data!</a>. In this course you will learn how to:
<ul>
<li>Create, shape and manage your data using the data.table package</li>
<li>Learn what other data-processing tools exist in R</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
Reordering column sometimes is necessary , however if your data frame is of several GBs it might be a overhead to create new data frame with new order .Data.Table provides features to overcome this . Now reorder your diamonds data.table’s column by sorting alphabetically<br>
<strong>Exercise 6</strong><br>
If you are not convinced with the powerful intuitive features of data.table till now , I am pretty sure you will by the end of THIS . Suppose I want to have a metric on diamonds where I want to find for each group of cut <code> maximum of x * mean of depth</code> and name it my_int_feature and also I want another metric which is <code>my_int_feature* maximum of y</code> again for each group of cut . This is achievable by chaining but also with a single operation without chaining which is the expected answer . </p>
<p><strong>Exercise 7</strong><br>
Suppose we want to merge iris and airquality,akin to the functionlaity of <code>rbind</code> . We want to do it fast and want to keep track of the rows with their original dataset , and keep all the columns of both the data set in the merged data set as well ,How do we achieve that</p>
<p><strong>Exercise 8</strong><br>
The Next 3 exercises are on rolling Join like features of data.table ,which is useful in time series like data . Create a data.table with the following<br>
<code>set.seed(1024)</code><br>
<code> x &lt;- data.frame(rep(letters[1:2],6),c(1,2,3,4,6,7),sample(100,6))</code><br>
<code>names(x) &lt;-  c(&quot;id&quot;,&quot;day&quot;,&quot;value&quot;)</code><br>
<code>test_dt &lt;- setDT(x)</code> </p>
<p>Now this mimics a sales data of 7 days for a and b . Notice that day 5 is not present for both a and b .This is not desirable in many situations , A common practise is to use the previous days data .How do we get previous days data for the id a ,You should ideally set keys and do it using join features </p>
<p><strong>Exercise 9</strong><br>
May be you dont want the previous day’s data ,you may want to copy the nearest value for day 5 ,How do we achieve that<br>
<strong>Exercise 10</strong><br>
Now there may be a case when you don’t want to copy any value if the date is beyond last observation .Use your answer for question 8 to find the value for day 5 and 9 for b ,Now since 9 falls beyond last observation of 7 you might want to avoid copying it .How do you explicitly tell your data.table to stop when it sees last observation and don’t copy previous value .This may not seem useful since you know that here 9 falls beyond 7 ,but imagine you have a series of data points and you don’t really want to copy data to observations after your last observation.This might come handy in such cases .</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/15/data-manipulation-with-data-table-part-1/" title="Data Manipulation with Data Table  -Part 1" target="_blank">Data Manipulation with Data Table -Part 1 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/26/shiny-application-layout-exercises-part-2/" title="Shiny Application Layouts Exercises (Part-2)" target="_blank">Shiny Application Layouts Exercises (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/01/22/bioinformatics-lessons-in-r-1-biostrings-exercises/" title="Bioinformatics Tutorial with Exercises in R (part 1)" target="_blank">Bioinformatics Tutorial with Exercises in R (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>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/J20FqiQy-LA" height="1">