How to return a logical value for a t test based on 0.05 level of significance in R?

R ProgrammingServer Side ProgrammingProgramming

<p>To return a logical value for a t test based on 0.05 level of significance in R, we can follow the below steps &minus;</p><ul class="list"><li>First of all, create a data frame with one column.</li><li>Apply t.test function with ifelse to return logical value based on 0.05 level of significance.</li></ul><h2>Example1</h2><p style=""><strong>Create the data frame</strong></p><p>Let&#39;s create a data frame as shown below &minus;</p><p><a class="demo" href="http://tpcg.io/l62pxeF0 " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">x&lt;-rnorm(20) df1&lt;-data.frame(x) df1</pre><p>On executing, the above script generates the below output(this output will vary on your system due to randomization) &minus;</p><pre class="result notranslate" style="">&nbsp; &nbsp; &nbsp; x 1 0.44038858 2 1.08938356 3 -0.23627885 4 0.73953751 5 -0.55476732 6 0.97726848 7 0.25612436 8 -1.89827676 9 0.50333232 10 0.55482166 11 1.83279952 12 0.93609228 13 0.35048901 14 0.05136088 15 -0.89102106 16 1.06392349 17 -0.15777431 18 0.45506977 19 1.43752763 20 1.27393923</pre><h2>Apply t.test to get the logical return</h2><p style="">Using t.test with ifelse to return logical output based on significance level 0.05 for less than alternative &minus;</p><p><a class="demo" href="http://tpcg.io/fornHGwA " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">x&lt;-rnorm(20) df1&lt;-data.frame(x) ifelse(t.test(df1$x,mu=10,alternative=&quot;less&quot;)[[&quot;p.value&quot;]]&lt;0.05,&quot;Yes&quot;,&quot;No&quot;)</pre><h3 style="">Output</h3><pre class="result notranslate">[1] &quot;Yes&quot;</pre><h2 style="">Example 2</h2><p style=""><strong>Create the data frame</strong></p><p>Let&#39;s create a data frame as shown below &minus;</p><p><a class="demo" href="http://tpcg.io/MNQZU7Ks " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">y&lt;-rpois(20,5) df2&lt;-data.frame(y) df2</pre><p>On executing, the above script generates the below output(this output will vary on your system due to randomization) &minus;</p><pre class="result notranslate" style="">&nbsp; y 1 5 2 5 3 5 4 3 5 8 6 3 7 5 8 8 9 7 10 6 11 2 12 7 13 3 14 4 15 5 16 6 17 11 18 9 19 4 20 6</pre><h2>Apply t.test to get the logical return</h2><p style="">Using t.test with ifelse to return logical output based on significance level 0.05 for greater than alternative &minus;</p><p style=""><a class="demo" href="http://tpcg.io/cmXgiint " rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">y&lt;-rpois(20,5) df2&lt;-data.frame(y) ifelse(t.test(df2$y,mu=10,alternative=&quot;greater&quot;)[[&quot;p.value&quot;]]&lt;0.05,&quot;Yes&quot;,&quot;No&quot;)</pre><h3 style="">Output</h3><pre class="result notranslate">[1] &quot;No&quot;</pre>
raja
Updated on 14-Aug-2021 08:09:44

Advertisements