<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-table-by-example-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://mathewanalytics.com/2017/09/27/data-table-by-example-part-2/"> R – Mathew Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In part <a rel="nofollow" href="https://mathewanalytics.com/2017/09/26/data-table-by-example-part-1/" target="_blank">one</a>, I provided an initial walk through of some nice features that are available within the data.table package. In particular, we saw how to filter data and get a count of rows by the date.</p>
<pre>
dat = fread(&quot;rows.csv&quot;)
names(dat) &lt;- gsub(&quot; &quot;, &quot;_&quot;, names(dat))
dat[1:3]
</pre>
<p>Let us now add a few columns to our dataset on reported crimes in the city of Chicago. There are many ways to do do this but they involve the use of the := operator. Since data.table updates values by reference, we do not need to save the results as another variable. This is a very desirable feature.</p>
<pre>
dat[, c(&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;) := sample(1:50, nrow(dat), replace=TRUE)]

dat[, `:=`(value1 = sample(1:50, nrow(dat), replace=TRUE),
           value2 = sample(1:50, nrow(dat), replace=TRUE),
           value3 = sample(1:50, nrow(dat), replace=TRUE))]
</pre>
<p>You can also just use the traditional base R solution for adding new columns as data.tables are also data frames.</p>
<pre>
dat$value1 &lt;- sample(1:50, nrow(dat), replace=TRUE)  
dat$value2 &lt;- sample(1:50, nrow(dat), replace=TRUE)  
dat$value3 &lt;- sample(1:50, nrow(dat), replace=TRUE)  
</pre>
<p>In any case, we now have three new columns with randomly selected values between 1 and 50. We can now look to summarize these values and see how they differ across the primary arrest type and other categorical variables.</p>
<pre>
dat[, .(mean = mean(value1, na.rm = TRUE),
        median = median(value1, na.rm = TRUE),
        min = min(value1, na.rm = TRUE),
        max = max(value1, na.rm = TRUE))]

dat[Primary_Type==&quot;PROSTITUTION&quot;, 
                  .(mean = mean(value1, na.rm = TRUE),
                    median = median(value1, na.rm = TRUE),
                    min = min(value1, na.rm = TRUE),
                    max = max(value1, na.rm = TRUE))]
</pre>
<p>The above code allows us to get the mean, median, min, and max values from the value1 column for all rows and for when the primary type is prostitution. To get these summaries across multiple columns, we can use lapply, .SD, and .SDcols.</p>
<pre>
dat[, lapply(.SD, mean, na.rm=TRUE), .SDcols=c(&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;)] 

dat[, lapply(.SD, mean, na.rm=TRUE), by=Primary_Type, .SDcols=c(&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;)][1:4] 
</pre>
<p>But wait, that only provides me with the mean of a single value column. If you want to apply multiple functions to columns, the user is required to write a function that can then be used within lapply.</p>
<pre>
my.summary &lt;- function(x){
   c(mean = mean(x), 
     median = median(x),
     min = min(x),
     max = max(x))
}

dat[, sapply(.SD, my.summary), .SDcols=c(&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;)]
</pre>
<p>Perfect! As you can see, the syntax is concise and is very easy to work with.</p>
<p>In the next part, I’ll cover a few advanced features like get() and {}.</p>
<p> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mathewanalytics.wordpress.com/838/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/mathewanalytics.wordpress.com/838/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></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="https://mathewanalytics.com/2017/09/27/data-table-by-example-part-2/"> R – Mathew Analytics</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/9RyGxheWiZQ" height="1"></a></div>