<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/r-simple-for-complex-tasks-complex-for-simple-tasks/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ekonometrics.blogspot.com/2018/03/r-simple-for-complex-tasks-complex-for.html"> eKonometrics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div dir="ltr">
<div><a rel="nofollow" href="https://i2.wp.com/www.r-project.org/Rlogo.png?ssl=1" target="_blank"><img border="0" data-original-height="155" src="https://i2.wp.com/www.r-project.org/Rlogo.png?resize=200%2C155&ssl=1" data-recalc-dims="1" data-original-width="200"></a></div>
<div>When it comes to undertaking complex data science projects, R is the preferred choice for many. Why? Because handling complex tasks is simpler in R than other comparable platforms.</div>
<div></div>
<div></div>
<div>Regrettably, the same is not true for performing simpler tasks, which I would argue is rather complex in base R. Hence, the title — R: simple for complex tasks, complex for simple tasks.</div>
<div></div>
<div>Consider a simple, yet mandatory, task of generating summary statistics for a research project involving tabular data. In most social science disciplines, summary statistics for continuous variables require generating mean, standard deviation, number of observations, and perhaps minimum and maximum. One would have hoped to see a function in base R to generate such table. But there isn’t one. </div>
<div></div>
<div>Of course, several user-written packages, such as <b><span>psyche</span></b>, can generate descriptive statistics in a tabular format. However, this requires one to have advanced knowledge of R and the capabilities hidden in specialized packages whose number now exceed 12,000 (as of March 2018). Keeping abreast of the functionality embedded in user-written packages is time-consuming. </div>
<div></div>
<div>Some would argue that the <b><span>summary</span></b> command in base R is an option. I humbly disagree. </div>
<div></div>
<div>First, the output from <b><span>summary</span></b> is not in a tabular format that one could just copy and paste into a document. It would require significant processing before a simple table with summary statistics for more than one continuous variable could be generated. Second, <b><span>summary</span></b> command does not report standard deviation.</div>
<div></div>
<div>I teach business analytics to <a rel="nofollow" href="https://www.ryerson.ca/tedrogersschool/bm/programs/real-estate-management/murtaza-haider/" target="_blank">undergraduate and MBA students</a>. While business students need to know statistics, they are not studying to become statisticians. Their goal in life is to be informed and proficient consumers of statistical analysis.</div>
<div></div>
<div>So, imagine an undergraduate class with 150 students learning to generate a simple table that reports summary statistics for more than one continuous variable. The simple task requires knowledge of several R commands. By the time one teaches these commands to students, most have made up their mind to do the analysis instead in Microsoft Excel instead. </div>
<div></div>
<div>Had there been a simple command to generate descriptive statistics in base R, this would not be a challenge for instructors trying to bring thousands more into R’s fold.</div>
<div></div>
<div>In the following paragraphs, I will illustrate the challenge with an example and identify an R package that generates a simple table of descriptive statistics.</div>
<div></div>
<div>I use <b><span>mtcars</span></b> dataset, which is available with R. The following commands load the dataset and display the first few observations with all the variables.</div>
<div></div>
<div><b><span>data(mtcars)</span></b></div>
<div><b><span>head(mtcars)</span></b></div>
<div></div>
<div>As stated earlier, one can use <b><span>summary</span></b> command to produce descriptive statistics. </div>
<div></div>
<div><b><span>summary(mtcars)</span></b></div>
<div></div>
<div>Let’s say one would like to generate descriptive statistics including mean, standard deviation, and the number of observations for the following continuous variables: <b>mpg, disp, and hp</b>. One can use the <b><span>sapply</span></b> command and generate the three statistics separately and combined them later using the <b><span>cbind</span></b> command.</div>
<div></div>
<div>The following command will create a vector of means.</div>
<div></div>
<div><b><span>mean.cars = with(mtcars, sapply(mtcars[c(“mpg”, “disp”,  “hp”)], mean))</span></b></div>
<div></div>
<div>Note that the above syntax requires someone learning R to know the following:</div>
<div></div>
<div>1.<span>    </span>Either to attach the dataset or to use <b><span>with</span></b> command so that <b><span>sapply</span></b> could recognize variables.</div>
<div>2.<span>    </span>Knowledge of subsetting variables in R</div>
<div>3.<span>    </span>Familiarity with <b><span>c</span></b> to combine variables </div>
<div>4.<span>    </span>Being aware of enclosing variable names in quotes</div>
<div></div>
<div>We can use similar syntax to determine standard deviation and the number of observations.</div>
<div></div>
<div><b><span>sd.cars = with(mtcars, sapply(mtcars[c(“mpg”, “disp”,  “hp”)], sd)); sd.cars</span></b></div>
<div><b><span>n.cars = with(mtcars, sapply(mtcars[c(“mpg”, “disp”,  “hp”)], length)); n.cars</span></b></div>
<div></div>
<div>Note that the user needs to know that the command for number of observations is <b><span>length</span></b> and for standard deviation is <b><span>sd</span></b>.</div>
<div></div>
<div>Once we have the three vectors, we can combine them using <b><span>cbind</span></b> that generates the following table. </div>
<div></div>
<div><b><span>cbind(n.cars, mean.cars, sd.cars)</span></b></div>
<div></div>
<div><span><span>     n.cars mean.cars    sd.cars</span></span></div>
<div><span><span>mpg      32  20.09062   6.026948</span></span></div>
<div><span><span>disp     32 230.72188 123.938694</span></span></div>
<div><span><span>hp       32 146.68750  68.562868</span></span><span></span></div>
<div></div>
<div>Again, one needs to know the <b><span>round</span></b> command to restrict the output to a specific number of decimals. See below the output with two decimal points.</div>
<div></div>
<div><b>round(cbind(n.cars, mean.cars, sd.cars),2)</b></div>
<div></div>
<div><span>     n.cars mean.cars sd.cars</span></div>
<div><span>mpg      32     20.09    6.03</span></div>
<div><span>disp     32    230.72  123.94</span></div>
<div><span>hp       32    146.69   68.56</span></div>
<div></div>
<div>One can indeed use a custom function to generate the same with one command. See below.</div>
<div></div>
<div><b><span>round(with(mtcars, t(sapply(mtcars[c(“mpg”, “disp”,  “hp”)], </span></b></div>
<div><b><span>                    function(x) c(n=length(x), avg=mean(x),</span></b></div>
<div><b><span>                    stdev=sd(x))</span></b><b><span>))), 2)</span></b></div>
<div></div>
<div><span>      n    avg  stdev</span></div>
<div><span>mpg  32  20.09   6.03</span></div>
<div><span>disp 32 230.72 123.94</span></div>
<div><span>hp   32 146.69  68.56</span></div>
<div></div>
<div>But the question I have for my fellow instructors is the following. How likely is an undergraduate student taking an introductory course in statistical analysis to be enthused about R if the simplest of the tasks need multiple lines of codes? A simple function in base R could keep students focussed on interpreting data rather than worrying about missing a comma or a parenthesis.</div>
<div></div>
<div><b><span>stargazer</span></b>* is an R package that simplifies this task. Here is the output from <b><span>stargazer</span></b>.</div>
<div><b><span><span><br></span></span></b></div>
<div><b><span><span>library(stargazer)</span></span></b></div>
<pre><span><b><span><span>stargazer(mtcars[c(&quot;mpg&quot;, &quot;disp&quot;,  &quot;hp&quot;)], type=&quot;text&quot;)</span></span></b></span></pre>
<pre><br></pre>
<pre>============================================<br>Statistic N   Mean   St. Dev.  Min     Max  <br>--------------------------------------------<br>mpg       32 20.091   6.027   10.400 33.900 <br>disp      32 230.722 123.939  71.100 472.000<br>hp        32 146.688  68.563    52     335  <br>--------------------------------------------</pre>
<pre><br></pre>
<div>A simple task, I argue, should be accomplished simply. My plea will be to include in base R a simple command that may generate the above table with a command as simple as the one below:</div>
<div></div>
<div><b><span>descriptives(mpg, disp, hp)</span></b></div>
<div></div>
<div></div>
<div><i><span>*  Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.  </span></i><span lang="FR"><i><span>R package version 5.2. <a rel="nofollow" href="http://cran.r-project.org/package=stargazer" target="_blank">http://CRAN.R-project.org/package=stargazer</a></span></i></span></div>
<div></div>
<p></p>
<div></div>
</div>
	<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://ekonometrics.blogspot.com/2018/03/r-simple-for-complex-tasks-complex-for.html"> eKonometrics</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/E1chjVqMUXk" height="1"></a></div>