<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/a-lazy-function/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/Cillianmacaodh/~3/wxbIdWh9RV8/a-lazy-function.html"> CillianMacAodh</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It has been quite a while since I posted, but I haven’t been idle, I completed my PhD since the last post, and I’m due to graduate next Thursday. I am also delighted to have recently been added to <a rel="nofollow" href="https://www.r-bloggers.com/" target="_blank">R-bloggers.com</a> so I’m keen to get back into it.</p>
<div>
<h2>A Lazy Function</h2>
<p>I have already written 2 posts about writing functions, and I will try to diversify my content. That said, I won’t refrain from sharing something that has been helpful to me. The function(s) I describe in this post is an artefact left over from before I started using R Markdown. It is a product of its time but may still be of use to people who haven’t switched to R Markdown yet. It is lazy (and quite imperfect) solution to a tedious task.</p>
<div>
<h3>The Problem</h3>
<p>At the time I wrote this function I was using <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> for my statistics and <a rel="nofollow" href="https://www.libreoffice.org/" target="_blank">Libreoffice</a> for writing. I would run a test in R and then write it up in Libreoffice. Each value that needed reporting had to be transferred from my R output to Libreoffice – and for each test there are a number of values that need reporting. Writing up these tests is pretty formulaic. There’s a set structure to the sentence, for example writing up a t-test with a significant result nearly always looks something like this:</p>
<p>An independent samples t-test revealed a significant difference in X between the Y sample, (<em>M</em> = [ ], <em>SD</em> = [ ]), and the Z sample, (<em>M</em> = [ ], SD = [ ]), <em>t</em>([df]) = [ ], <em>p</em> = [ ].</p>
<p>And the write up of a non-significant result looks something like this:</p>
<p>An independent samples t-test revealed no significant difference in X between the Y sample, (<em>M</em> = [ ], <em>SD</em> = [ ]), and the Z sample, (<em>M</em> = [ ], SD = [ ]), <em>t</em>([df]) = [ ], <em>p</em> = [ ].</p>
<p>Seven values (the square [ ] brackets) need to be reported for this single test. Whether you copy and paste or type each value, the reporting of such tests can be very tedious, and leave you prone to errors in reporting.</p>
</div>
<div>
<h3>The Solution</h3>
<p>In order to make reporting values easier (and more accurate) I wrote the <code>t_paragraph()</code> function (and the related <code>t_paired_paragraph()</code> function). This provided an output that I could copy and paste into a Word (Libreoffice) document. This function is part of the <a rel="nofollow" href="https://github.com/cillianmiltown/R_desnum" target="_blank"><code>desnum</code></a><a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fn1" target="_blank"><sup>1</sup></a> package <span>(McHugh, 2017)</span>.</p>
<div>
<h4>The <code>t_parapgraph()</code> Function</h4>
<p>The <code>t_parapgraph()</code> function runs a t-test and generates an output that can be copied and pasted into a word document. The code for the function is as follows:</p>
<pre><code><span># Create the function t_paragraph with arguments x, y, and measure</span>
<span># x is the dependent variable</span>
<span># y is the independent (grouping) variable</span>
<span># measure is the name of dependent variable inputted as string</span>

t_paragraph &lt;- <span>function</span> (x, y, measure){
  
  <span># Run a t-test and store it as an object t</span>
  
  t &lt;- t.test(x ~ y)
  
  
  <span># If your grouping variable has labelled levels, the next line will store them for reporting at a later stage</span>
  
  labels &lt;- levels(y)
  
  <span># Create an object for each value to be reported</span>
  
  tsl &lt;- as.vector(t$statistic)
  ts &lt;- round(tsl, digits = <span>3</span>)
  tpl &lt;- as.vector(t$p.value)
  tp &lt;- round(tpl, digits = <span>3</span>)
  d_fl &lt;- as.vector(t$parameter)
  d_f &lt;- round(d_fl, digits = <span>2</span>)
  ml &lt;- as.vector(tapply(x, y, mean))
  m &lt;- round(ml, digits = <span>2</span>)
  sdl &lt;- as.vector(tapply(x, y, sd))
  sd &lt;- round(sdl, digits = <span>2</span>)
  
  <span># Use print(paste0()) to combine the objects above and create two potential outputs</span>
  <span># The output that is generated will depend on the result of the test</span>
  
  
  <span># wording if significant difference is observed</span>
  
  <span>if</span> (tp &lt; <span>0.05</span>) 
    print(paste0(<span>&quot;An independent samples t-test revealed a significant difference in &quot;</span>, 
                 measure, <span>&quot; between the &quot;</span>, labels[<span>1</span>], <span>&quot; sample, (M = &quot;</span>, 
                 m[<span>1</span>], <span>&quot;, SD = &quot;</span>, sd[<span>1</span>], <span>&quot;), and the &quot;</span>, labels[<span>2</span>], 
                 <span>&quot; sample, (M =&quot;</span>, m[<span>2</span>], <span>&quot;, SD =&quot;</span>, sd[<span>2</span>], <span>&quot;), t(&quot;</span>, 
                 d_f, <span>&quot;) = &quot;</span>, ts, <span>&quot;, p = &quot;</span>, tp, <span>&quot;.&quot;</span>), quote = <span>FALSE</span>, 
          digits = <span>2</span>)
  
  <span># wording if no significant difference is observed </span>
  
  <span>if</span> (tp > <span>0.05</span>) 
    print(paste0(<span>&quot;An independent samples t-test revealed no difference in &quot;</span>, 
                 measure, <span>&quot; between the &quot;</span>, labels[<span>1</span>], <span>&quot; sample, (M = &quot;</span>, 
                 m[<span>1</span>], <span>&quot;, SD = &quot;</span>, sd[<span>1</span>], <span>&quot;), and the &quot;</span>, labels[<span>2</span>], 
                 <span>&quot; sample, (M = &quot;</span>, m[<span>2</span>], <span>&quot;, SD =&quot;</span>, sd[<span>2</span>], <span>&quot;), t(&quot;</span>, 
                 d_f, <span>&quot;) = &quot;</span>, ts, <span>&quot;, p = &quot;</span>, tp, <span>&quot;.&quot;</span>), quote = <span>FALSE</span>, 
          digits = <span>2</span>)
}</code></pre>
<p>When using <code>t_paragraph()</code>, <code>x</code> is your DV, <code>y</code> is your grouping variable while <code>measure</code> is a string value that the name of the dependent variable. To illustrate the function I’ll use the <code>mtcars</code> dataset.</p>
</div>
<div>
<h4>Applications of the <code>t_parapgraph()</code> Function</h4>
<p>The <code>mtcars</code> dataset is comes with R. For information on it simply type <code>help(mtcars)</code>. The variables of interest here are <code>am</code>(transmission; 0 = automatic, 1 = manual), <code>mpg</code> (miles per gallon), <code>qsec</code> (1/4 mile time). The two questions I’m going to look at are:</p>
<ol>
<li>Is there a difference in miles per gallon depending on transmission?</li>
<li>Is there a difference in 1/4 mile time depending on transmission?</li>
</ol>
<p>Before running the test it is a good idea to look at the data<a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fn2" target="_blank"><sup>2</sup></a>. Because we’re going to look at differences between groups we want to run descriptives for each group separately. To do this I’m going to combine the the <code>descriptives()</code> function which I previously covered <a rel="nofollow" href="https://cillianmacaodh.blogspot.com/2017/07/writing-functions-part-one-codewhite.html" target="_blank">here</a> (also part of the <code>desnum</code> package) and the <code>tapply()</code> function.</p>
<p>The <code>tapply()</code> function allows you to run a function on subsets of a dataset using a grouping variable (or index). The arguments are as follows <code>tapply(vector, index, function)</code>. <code>vector</code> is the variable you want to pass through <code>function</code>; and <code>index</code> is the grouping variable. The examples below will make this clearer.</p>
<p>We want to run descriptives on <code>mtcars$mpg</code> and on <code>mtcars$qsec</code> and for each we want to group by transmission (<code>mtcars$am</code>). This can be done using <code>tapply()</code> and <code>descriptives()</code> together as follows:</p>
<pre><code>tapply(mtcars$mpg, mtcars$am, descriptives)</code></pre>
<pre><code>## $`0`
##       mean       sd  min  max len
## 1 17.14737 3.833966 10.4 24.4  19
## 
## $`1`
##       mean       sd min  max len
## 1 24.39231 6.166504  15 33.9  13</code></pre>
<p>Recall that 0 = automatic, and 1 = manual. Replace <code>mpg</code> with <code>qsec</code> and run again:</p>
<pre><code>tapply(mtcars$qsec, mtcars$am, descriptives)</code></pre>
<pre><code>## $`0`
##       mean       sd   min  max len
## 1 18.18316 1.751308 15.41 22.9  19
## 
## $`1`
##    mean       sd  min  max len
## 1 17.36 1.792359 14.5 19.9  13</code></pre>
</div>
</div>
<div>
<h3>Running <code>t_paragraph()</code></h3>
<p>Now that we know the values for automatic vs manual cars we can run our t-tests using <code>t_paragraph()</code>. Our first question:</p>
<blockquote><p>Is there a difference in miles per gallon depeding on transmission?</p></blockquote>
<pre><code>t_paragraph(mtcars$mpg, mtcars$am, <span>&quot;miles per gallon&quot;</span>)</code></pre>
<pre><code>## [1] An independent samples t-test revealed a significant difference in miles per gallon between the  sample, (M = 17.15, SD = 3.83), and the  sample, (M =24.39, SD =6.17), t(18.33) = -3.767, p = 0.001.</code></pre>
<p>There is a difference, and the output above can be copied and pasted into a word document with minimal changes required.</p>
<p>Our second question was:</p>
<blockquote><p>Is there a difference in 1/4 mile time depending on transmission?</p></blockquote>
<pre><code>t_paragraph(mtcars$qsec, mtcars$am, <span>&quot;quarter-mile time&quot;</span>)</code></pre>
<pre><code>## [1] An independent samples t-test revealed no difference in quarter-mile time between the  sample, (M = 18.18, SD = 1.75), and the  sample, (M = 17.36, SD =1.79), t(25.53) = 1.288, p = 0.209.</code></pre>
<p>This time there was no significant difference, and again the output can be copied and pasted into word with minimal changes.</p>
</div>
<div>
<h3>Limitations</h3>
<p>The function described was written a long time ago, and could be updated. However I no longer copy and paste into word (having switched to R markdown instead). The reporting of the p value is not always to APA standards. If p is &lt; .001 this is what should be reported. The code for <code>t_paragraph()</code> could be updated to include the <code>p_report</code> function (described <a rel="nofollow" href="https://cillianmacaodh.blogspot.com/2017/08/writing-functions-part-two.html" target="_blank">here</a>) which would address this. Another limitation is that the formatting of the text isn’t perfect, the letters (N,M,SD,t,p) should all be italicised, but having to manually fix this formatting is still easier than manually transferring individual values.</p>
</div>
<div>
<h3>Conclusion</h3>
<p>Despite the limitations the functions <code>t_paragraph()</code> and <code>t_paired_paragraph()</code><a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fn3" target="_blank"><sup>3</sup></a> have made my life easier. I still use them occasionally. I hope they can be of use to anyone who is using R but has not switched to R Markdown yet.</p>
</div>
<div>
<h3>References</h3>
<div>
<div>
<p>McHugh, C. (2017). <em>Desnum: Creates some useful functions</em>.</p>
</div>
</div>
</div>
</div>
<div>
<hr>
<ol>
<li>To install <code>desnum</code> just run <code>devtools::install_github(&quot;cillianmiltown/R_desnum&quot;)</code><a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fnref1" target="_blank"><img alt="↩" src="https://s.w.org/images/core/emoji/2.4/72x72/21a9.png" class="wp-smiley"></a></li>
<li>In this case this is particularly useful because there are no value labels for <code>mtcars$am</code>, so it won’t be clear from the output which values refer to the automatic group and which refer to the manual group. Running descriptives will help with this.<a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fnref2" target="_blank"><img alt="↩" src="https://s.w.org/images/core/emoji/2.4/72x72/21a9.png" class="wp-smiley"></a></li>
<li>If you want to see the code for <code>t_paired_paragraph()</code> just load <code>desnum</code> and run <code>t_paired_paragraph</code> (without parenthesis)<a rel="nofollow" href="http://cillianmacaodh.blogspot.com/2018/10/a-lazy-function.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Cillianmacaodh+%28CillianMacAodh%29#fnref3" target="_blank"><img alt="↩" src="https://s.w.org/images/core/emoji/2.4/72x72/21a9.png" class="wp-smiley"></a></li>
</ol>
</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://feedproxy.google.com/~r/Cillianmacaodh/~3/wxbIdWh9RV8/a-lazy-function.html"> CillianMacAodh</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/Ov52dLetJF8" height="1"></a></div>