<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/" data-counturl="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/" data-count="horizontal" data-text="Best packages for data manipulation in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/best-packages-for-data-manipulation-in-r/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>There are 2 packages that make data manipulation in R fun. These are <code>dplyr</code> and <code>data.table</code>. Both packages have their strengths. While <code>dplyr</code> is more elegant and resembles natural language, <code>data.table</code> is succinct and we can do a lot with <code>data.table</code> in just a single line. Further, <code>data.table</code> is, in some cases, faster (see benchmark <a target="_blank" rel="nofollow" href="https://github.com/Rdatatable/data.table/wiki/Benchmarks-%3A-Grouping">here</a>) and it may be a go-to package when performance and memory are constraints. You can read comparison of <code>dplyr</code> and <code>data.table</code> from <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/21435339/data-table-vs-dplyr-can-one-do-something-well-the-other-cant-or-does-poorly">Stack Overflow</a> and <a target="_blank" rel="nofollow" href="https://www.quora.com/Which-is-better-to-use-for-data-manipulation-dplyr-package-or-data-table-library">Quora</a>.</p>
<p>You can get reference manual and vignettes for <code>data.table</code> <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/data.table/index.html">here</a> and for <code>dplyr</code> <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/index.html">here</a>. You can read <a target="_blank" href="http://datascienceplus.com/data-manipulation-with-dplyr/">other tutorial</a> about <code>dplyr</code> published at DataScience+</p>
<h2>Background</h2>
<p>I am a long time <code>dplyr</code> and <code>data.table</code> user for my data manipulation tasks. For someone who knows one of these packages, I thought it could help to show codes that perform the same tasks in both packages to help them quickly study the other. If you know either package and have interest to study the other, this post is for you.</p>
<h3>dplyr</h3>
<p>dplyr has 5 verbs which make up the majority of the data manipulation tasks we perform. Select: used to select one or more columns; Filter: used to select some rows based on specific criteria; Arrange: used to sort data based on one or more columns in ascending or descending order; Mutate: used to add new columns to our data; Summarise: used to create chunks from our data.</p>
<h3>data.table</h3>
<p>data.table has a very succinct general format: DT[<em>i, j, by</em>], which is interpreted as: Take DT, subset rows using <em>i</em>, then calculate <em>j</em> grouped by <em>by</em>.</p>
<h2>Data manipulation</h2>
<p>First we will install some packages for our project.</p>
<pre>
library(dplyr)
library(data.table)
library(lubridate)
library(jsonlite)
library(tidyr)
library(ggplot2)
library(compare)
</pre>
<p>The data we will use here is from <a target="_blank" rel="nofollow" href="https://www.data.gov/">DATA.GOV</a>. It is Medicare Hospital Spending by Claim and it can be downloaded from <a target="_blank" rel="nofollow" href="https://catalog.data.gov/dataset/medicare-hospital-spending-by-claim-61b57">here</a>. Let’s download the data in <em>JSON</em> format using the <code>fromJSON</code> function from the <em>jsonlite</em> package. Since <em>JSON</em> is a very common data format used for asynchronous browser/server communication, it is good if you understand the lines of code below used to get the data. You can get an introductory tutorial on how to use the jsonlite package to work with JSON data <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html">here</a> and <a target="_blank" href="http://datascienceplus.com/introduction-to-time-series-with-json-data/">here</a>. However, if you want to focus only on the <code>data.table</code> and <code>dplyr</code> commands, you can safely just run the codes in the two cells below and ignore the details.</p>
<pre>
spending=fromJSON(&quot;https://data.medicare.gov/api/views/nrth-mfg3/rows.json?accessType=DOWNLOAD&quot;)
names(spending)<em>
&quot;meta&quot; &quot;data&quot; </em>

meta=spending$meta
hospital_spending=data.frame(spending$data)
colnames(hospital_spending)=make.names(meta$view$columns$name)
hospital_spending=select(hospital_spending,-c(sid:meta))

glimpse(hospital_spending)<em>
Observations: 70598
Variables:
$ Hospital.Name (fctr) SOUTHEAST ALABAMA MEDICAL CENT...
$ Provider.Number. (fctr) 010001, 010001, 010001, 010001...
$ State (fctr) AL, AL, AL, AL, AL, AL, AL, AL...
$ Period (fctr) 1 to 3 days Prior to Index Hos...
$ Claim.Type (fctr) Home Health Agency, Hospice, I...
$ Avg.Spending.Per.Episode..Hospital. (fctr) 12, 1, 6, 160, 1, 6, 462, 0, 0...
$ Avg.Spending.Per.Episode..State. (fctr) 14, 1, 6, 85, 2, 9, 492, 0, 0,...
$ Avg.Spending.Per.Episode..Nation. (fctr) 13, 1, 5, 117, 2, 9, 532, 0, 0...
$ Percent.of.Spending..Hospital. (fctr) 0.06, 0.01, 0.03, 0.84, 0.01, ...
$ Percent.of.Spending..State. (fctr) 0.07, 0.01, 0.03, 0.46, 0.01, ...
$ Percent.of.Spending..Nation. (fctr) 0.07, 0.00, 0.03, 0.58, 0.01, ...
$ Measure.Start.Date (fctr) 2014-01-01T00:00:00, 2014-01-0...
$ Measure.End.Date (fctr) 2014-12-31T00:00:00, 2014-12-3...</em></pre>
<p>As shown above, all columns are imported as factors and let’s change the columns that contain numeric values to numeric.</p>
<pre>cols = 6:11; # These are the columns to be changed to numeric.
hospital_spending[,cols] &lt;- lapply(hospital_spending[,cols], as.numeric)</pre>
<p>The last two columns are measure start date and measure end date. So, let’s use the <code>lubridate</code> package to correct the classes of these columns.</p>
<pre>cols = 12:13; # These are the columns to be changed to dates.
hospital_spending[,cols] &lt;- lapply(hospital_spending[,cols], ymd_hms)</pre>
<p>Now, let’s check if the columns have the classes we want.</p>
<pre>sapply(hospital_spending, class)<em>
$Hospital.Name
&quot;factor&quot;
$Provider.Number.
&quot;factor&quot;
$State
&quot;factor&quot;
$Period
&quot;factor&quot;
$Claim.Type
&quot;factor&quot;
$Avg.Spending.Per.Episode..Hospital.
&quot;numeric&quot;
$Avg.Spending.Per.Episode..State.
&quot;numeric&quot;
$Avg.Spending.Per.Episode..Nation.
&quot;numeric&quot;
$Percent.of.Spending..Hospital.
&quot;numeric&quot;
$Percent.of.Spending..State.
&quot;numeric&quot;
$Percent.of.Spending..Nation.
&quot;numeric&quot;
$Measure.Start.Date
&quot;POSIXct&quot; &quot;POSIXt&quot; $Measure.End.Date
&quot;POSIXct&quot; &quot;POSIXt&quot; </em></pre>
<h2>Create data table</h2>
<p>We can create a data.table using the <code>data.table()</code> function.</p>
<pre>hospital_spending_DT = data.table(hospital_spending)
class(hospital_spending_DT)<em>
&quot;data.table&quot; &quot;data.frame&quot; </em></pre>
<h2>Select certain columns of data</h2>
<p>To select columns, we use the verb <code>select</code> in <code>dplyr</code>. In <code>data.table</code>, on the other hand, we can specify the column names.</p>
<h3>Selecting one variable</h3>
<p>Let’s selet the “Hospital Name” variable</p>
<pre>from_dplyr = select(hospital_spending, Hospital.Name)
from_data_table = hospital_spending_DT[,.(Hospital.Name)]</pre>
<p>Now, let’s compare if the results from <code>dplyr</code> and <code>data.table</code> are the same.</p>
<pre>compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h3>Removing one variable</h3>
<pre>from_dplyr = select(hospital_spending, -Hospital.Name)
from_data_table = hospital_spending_DT[,!c(&quot;Hospital.Name&quot;),with=FALSE]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<p>we can also use <code>:=</code> function which modifies the input <code>data.table</code> by reference.<br>
We will use the <code>copy() </code>function, which deep copies the input object and therefore any subsequent update by reference operations performed on the copied object will not affect the original object.</p>
<pre>DT=copy(hospital_spending_DT)
DT=DT[,Hospital.Name:=NULL]
&quot;Hospital.Name&quot;%in%names(DT)<em>FALSE </em></pre>
<p>We can also remove many variables at once similarly:</p>
<pre>DT=copy(hospital_spending_DT)
DT=DT[,c(&quot;Hospital.Name&quot;,&quot;State&quot;,&quot;Measure.Start.Date&quot;,&quot;Measure.End.Date&quot;):=NULL]
c(&quot;Hospital.Name&quot;,&quot;State&quot;,&quot;Measure.Start.Date&quot;,&quot;Measure.End.Date&quot;)%in%names(DT)<em>
FALSE FALSE FALSE FALSE </em></pre>
<h3>Selecting multiple variables</h3>
<p>Let’s select the variables:<br>
Hospital.Name,State,Measure.Start.Date,and Measure.End.Date.</p>
<pre>
from_dplyr = select(hospital_spending, Hospital.Name,State,Measure.Start.Date,Measure.End.Date)
from_data_table = hospital_spending_DT[,.(Hospital.Name,State,Measure.Start.Date,Measure.End.Date)]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h3>Dropping multiple variables</h3>
<p>Now, let’s remove the variables Hospital.Name,State,Measure.Start.Date,and Measure.End.Date from the original data frame hospital_spending and the data.table hospital_spending_DT.</p>
<pre>from_dplyr = select(hospital_spending, -c(Hospital.Name,State,Measure.Start.Date,Measure.End.Date))
from_data_table = hospital_spending_DT[,!c(&quot;Hospital.Name&quot;,&quot;State&quot;,&quot;Measure.Start.Date&quot;,&quot;Measure.End.Date&quot;),with=FALSE]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<p><code>dplyr</code> has functions <code>contains()</code>, <code>starts_with()</code> and, <code>ends_with()</code> which we can use with the verb select. In <code>data.table</code>, we can use regular expressions. Let’s select columns that contain the word Date to demonstrate by example.</p>
<pre>from_dplyr = select(hospital_spending,contains(&quot;Date&quot;))
from_data_table = subset(hospital_spending_DT,select=grep(&quot;Date&quot;,names(hospital_spending_DT)))
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em>

names(from_dplyr)<em>
&quot;Measure.Start.Date&quot; &quot;Measure.End.Date&quot; </em></pre>
<h2>Rename columns</h2>
<pre>setnames(hospital_spending_DT,c(&quot;Hospital.Name&quot;, &quot;Measure.Start.Date&quot;,&quot;Measure.End.Date&quot;), c(&quot;Hospital&quot;,&quot;Start_Date&quot;,&quot;End_Date&quot;))
names(hospital_spending_DT)<em>
&quot;Hospital&quot; &quot;Provider.Number.&quot; &quot;State&quot; &quot;Period&quot; &quot;Claim.Type&quot; &quot;Avg.Spending.Per.Episode..Hospital.&quot; &quot;Avg.Spending.Per.Episode..State.&quot; &quot;Avg.Spending.Per.Episode..Nation.&quot; &quot;Percent.of.Spending..Hospital.&quot; &quot;Percent.of.Spending..State.&quot; &quot;Percent.of.Spending..Nation.&quot; &quot;Start_Date&quot; &quot;End_Date&quot; </em>

hospital_spending = rename(hospital_spending,Hospital= Hospital.Name, Start_Date=Measure.Start.Date,End_Date=Measure.End.Date)
compare(hospital_spending,hospital_spending_DT, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h2>Filtering data to select certain rows</h2>
<p>To filter data to select specific rows, we use the verb <code>filter</code> from <code>dplyr</code> with logical statements that could include regular expressions. In <code>data.table</code>, we need the logical statements only.</p>
<h3>Filter based on one variable</h3>
<pre>from_dplyr = filter(hospital_spending,State=='CA') # selecting rows for California
from_data_table = hospital_spending_DT[State=='CA']
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h3>Filter based on multiple variables</h3>
<pre>from_dplyr = filter(hospital_spending,State=='CA' &amp; Claim.Type!=&quot;Hospice&quot;) 
from_data_table = hospital_spending_DT[State=='CA' &amp; Claim.Type!=&quot;Hospice&quot;]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<pre>from_dplyr = filter(hospital_spending,State %in% c('CA','MA',&quot;TX&quot;)) 
from_data_table = hospital_spending_DT[State %in% c('CA','MA',&quot;TX&quot;)]
unique(from_dplyr$State)<em>
CA MA TX </em>

compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h2>Order data</h2>
<p>We use the verb <code>arrange</code> in <code>dplyr</code> to order the rows of data. We can order the rows by one or more variables. If we want descending, we have to use <code>desc()</code> as shown in the examples.The examples are self-explanatory on how to sort in ascending and descending order. Let’s sort using one variable.</p>
<h3>Ascending</h3>
<pre>from_dplyr = arrange(hospital_spending, State)
from_data_table = setorder(hospital_spending_DT, State)
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h3>Descending</h3>
<pre>from_dplyr = arrange(hospital_spending, desc(State))
from_data_table = setorder(hospital_spending_DT, -State)
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h3>Sorting with multiple variables</h3>
<p>Let’s sort with State in ascending order and End_Date in descending order.</p>
<pre>
from_dplyr = arrange(hospital_spending, State,desc(End_Date))
from_data_table = setorder(hospital_spending_DT, State,-End_Date)
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
dropped attributes</em></pre>
<h2>Adding/updating column(s)</h2>
<p>In <code>dplyr</code> we use the function <code>mutate()</code> to add columns. In <code>data.table</code>, we can Add/update a column by reference using <code>:=</code> in one line.</p>
<pre>from_dplyr = mutate(hospital_spending, diff=Avg.Spending.Per.Episode..State. - Avg.Spending.Per.Episode..Nation.)
from_data_table = copy(hospital_spending_DT)
from_data_table = from_data_table[,diff := Avg.Spending.Per.Episode..State. - Avg.Spending.Per.Episode..Nation.]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
sorted
renamed rows
dropped row names
dropped attributes</em></pre>
<pre>from_dplyr = mutate(hospital_spending, diff1=Avg.Spending.Per.Episode..State. - Avg.Spending.Per.Episode..Nation.,diff2=End_Date-Start_Date)
from_data_table = copy(hospital_spending_DT)
from_data_table = from_data_table[,c(&quot;diff1&quot;,&quot;diff2&quot;) := list(Avg.Spending.Per.Episode..State. - Avg.Spending.Per.Episode..Nation.,diff2=End_Date-Start_Date)]
compare(from_dplyr,from_data_table, allowAll=TRUE)
TRUE
dropped attributes</pre>
<h2>Summarizing columns</h2>
<p>We can use the <code>summarize()</code> function from<code> dplyr </code>to create summary statistics.</p>
<pre>summarize(hospital_spending,mean=mean(Avg.Spending.Per.Episode..Nation.))<em>
mean 8.772727</em>

hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Nation.))]<em>
mean 8.772727</em>

summarize(hospital_spending,mean=mean(Avg.Spending.Per.Episode..Nation.),
                            maximum=max(Avg.Spending.Per.Episode..Nation.),
                            minimum=min(Avg.Spending.Per.Episode..Nation.),
                            median=median(Avg.Spending.Per.Episode..Nation.))<em>
mean maximum minimum median
8.77 19 1 8.5</em>

hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Nation.),
                        maximum=max(Avg.Spending.Per.Episode..Nation.),
                        minimum=min(Avg.Spending.Per.Episode..Nation.),
                        median=median(Avg.Spending.Per.Episode..Nation.))]<em>
mean maximum minimum median
8.77 19 1 8.5</em></pre>
<p>We can calculate our summary statistics for some chunks separately. We use the function <code>group_by()</code> in <code>dplyr</code> and in <code>data.table</code>, we simply provide <code>by</code>.</p>
<pre>head(hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)),by=.(Hospital)])</pre>
<p><a title="" data-rel="lightbox-0" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt5.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt5-490x269.png?resize=456%2C250 490w, http://datascienceplus.com/wp-content/uploads/2016/05/dt5-300x165.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/dt5.png 548w" alt="dt5" class="wp-image-3772" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt5-490x269.png?resize=456%2C250" data-recalc-dims="1"></a></p>
<pre>
mygroup= group_by(hospital_spending,Hospital) 
from_dplyr = summarize(mygroup,mean=mean(Avg.Spending.Per.Episode..Hospital.))
from_data_table=hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)), by=.(Hospital)]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
sorted
renamed rows
dropped row names
dropped attributes</em></pre>
<p>We can also provide more than one grouping condition.</p>
<pre>head(hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)),
                          by=.(Hospital,State)])</pre>
<p><a title="" data-rel="lightbox-1" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt6.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt6-490x245.png?resize=456%2C228 490w, http://datascienceplus.com/wp-content/uploads/2016/05/dt6-600x300.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/dt6-300x150.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/dt6.png 610w" alt="dt6" class="wp-image-3773" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/dt6-490x245.png?resize=456%2C228" data-recalc-dims="1"></a></p>
<pre>
mygroup= group_by(hospital_spending,Hospital,State)
from_dplyr = summarize(mygroup,mean=mean(Avg.Spending.Per.Episode..Hospital.))
from_data_table=hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)), by=.(Hospital,State)]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
sorted
renamed rows
dropped row names
dropped attributes</em></pre>
<h2>Chaining</h2>
<p>With both <code>dplyr</code> and <code>data.table</code>, we can chain functions in succession. In <code>dplyr</code>, we use pipes from the <code>magrittr</code> package with <code>%>%</code> which is really cool. <code>%>% </code>takes the output from one function and feeds it to the first argument of the next function. In <code>data.table</code>, we can use <code>%>%</code> or <code>[</code> for chaining.</p>
<pre>
from_dplyr=hospital_spending%>%group_by(Hospital,State)%>%summarize(mean=mean(Avg.Spending.Per.Episode..Hospital.))
from_data_table=hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)), by=.(Hospital,State)]
compare(from_dplyr,from_data_table, allowAll=TRUE)<em>
TRUE
sorted
renamed rows
dropped row names
dropped attributes</em></pre>
<pre>hospital_spending%>%group_by(State)%>%summarize(mean=mean(Avg.Spending.Per.Episode..Hospital.))%>%
arrange(desc(mean))%>%head(10)%>%
        mutate(State = factor(State,levels = State[order(mean,decreasing =TRUE)]))%>%
          ggplot(aes(x=State,y=mean))+geom_bar(stat='identity',color='darkred',fill='skyblue')+
          xlab(&quot;&quot;)+ggtitle('Average Spending Per Episode by State')+
          ylab('Average')+ coord_cartesian(ylim = c(3800, 4000))</pre>
<p><a title="" data-rel="lightbox-2" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7-490x302.png?resize=456%2C281 490w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-768x474.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-1024x632.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-600x370.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-300x185.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7.png 1036w" alt="ddt7" class="wp-image-3774" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7-490x302.png?resize=456%2C281" data-recalc-dims="1"></a></p>
<pre>hospital_spending_DT[,.(mean=mean(Avg.Spending.Per.Episode..Hospital.)),
                                     by=.(State)][order(-mean)][1:10]%>% 
            mutate(State = factor(State,levels = State[order(mean,decreasing =TRUE)]))%>%
           ggplot(aes(x=State,y=mean))+geom_bar(stat='identity',color='darkred',fill='skyblue')+
          xlab(&quot;&quot;)+ggtitle('Average Spending Per Episode by State')+
          ylab('Average')+ coord_cartesian(ylim = c(3800, 4000))</pre>
<p><a title="" data-rel="lightbox-2" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7-490x302.png?resize=456%2C281 490w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-768x474.png 768w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-1024x632.png 1024w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-600x370.png 600w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7-300x185.png 300w, http://datascienceplus.com/wp-content/uploads/2016/05/ddt7.png 1036w" alt="ddt7" class="wp-image-3774" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/05/ddt7-490x302.png?resize=456%2C281" data-recalc-dims="1"></a></p>
<h2>Summary</h2>
<p>In this blog post, we saw how we can perform the same tasks using <code>data.table</code> and <code>dplyr</code> packages. Both packages have their strengths. While <code>dplyr</code> is more elegant and resembles natural language, <code>data.table</code> is succinct and we can do a lot with <code>data.table </code>in just a single line. Further, <code>data.table</code> is, in some cases, faster and it may be a go-to package when performance and memory are the constraints.</p>
<p>You can get the code for this blog post at my <a target="_blank" rel="nofollow" href="https://github.com/fissehab/dplyr.way_vs_data.table.way">GitHub</a> account.</p>
<p>This is enough for this post. If you have any questions or feedback, feel free to leave a comment.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toIdentify, describe, plot, and remove the outliers from the dataset" target="_blank" rel="bookmark" href="http://datascienceplus.com/identify-describe-plot-and-removing-the-outliers-from-the-dataset/">Identify, describe, plot, and remove the outliers from the dataset</a></li>
<li><a title="Permanent Link toLearn R By Intensive Practice – Part 2" target="_blank" rel="bookmark" href="http://datascienceplus.com/learn-r-by-intensive-practice-part-2/">Learn R By Intensive Practice – Part 2</a></li>
<li><a title="Permanent Link toWorking with databases in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/working-with-databases-in-r/">Working with databases in R</a></li>
<li><a title="Permanent Link toData manipulation with tidyr" target="_blank" rel="bookmark" href="http://datascienceplus.com/data-manipulation-with-tidyr/">Data manipulation with tidyr</a></li>
<li><a title="Permanent Link toBringing the powers of SQL into R" target="_blank" rel="bookmark" href="http://datascienceplus.com/bringing-the-powers-of-sql-into-r/">Bringing the powers of SQL into R</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=UP5HaSE_Ys8:sgixqQcBBig:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=UP5HaSE_Ys8:sgixqQcBBig:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=UP5HaSE_Ys8:sgixqQcBBig:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=UP5HaSE_Ys8:sgixqQcBBig:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/" data-counturl="http://www.r-bloggers.com/best-packages-for-data-manipulation-in-r/" data-count="vertical" data-text="Best packages for data manipulation in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/best-packages-for-data-manipulation-in-r/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/-p2Av3Jx3M0"></a></div>