Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First let's make some example data. For each sample, we have a unique ID, its experimental group, and some variables for which we want to calculate the mean and SD.</p> <pre><code>## Make a data frame called "Data" with five columns Data &lt;- as.data.frame(cbind(1:100, sample(1:2), rnorm(100), rnorm(100), rnorm(100), rnorm(100))) names(Data) &lt;- c("ID", "Group", "V1", "V2", "V3", "V4") ## Now we will take a peak at the top of our data frame &gt; head(Data) ID Group V1 V2 V3 V4 1 1 2 0.3681539 -0.5008400 1.2060665 -0.7352376 2 2 1 -0.1043180 2.2038190 -1.4367898 2.1961246 3 3 2 -0.2720279 -0.5923554 -1.4628190 -1.8776453 4 4 1 -2.3299662 -0.1216227 0.4200776 1.5504020 5 5 2 -0.3670578 -1.5903221 -0.6287083 -1.0543262 6 6 1 0.4840047 -0.3181554 -1.4596980 -0.4261827 </code></pre> <p>Now we can run a for loop through the variables, pull their means and SDs and p values, and dump them all in an object called "Results".</p> <pre><code>## Define object which will receive our results Results &lt;- NULL Results &lt;- as.data.frame(Results) ## Open for loop for (i in 3:6) { ## Run the t.test() and save it in a temporary object temp &lt;- t.test(Data[which(Data$Group == 1), i], Data[which(Data$Group == 2), i]) ## Put the name of our variable in our results object Results[i-2,1] &lt;- names(Data)[i] ## Group 1 mean and SD Results[i-2,2] &lt;- temp$estimate[1] Results[i-2,3] &lt;- sd(Data[which(Data$Group == 1), i]) ## Group 2 mean and SD Results[i-2,4] &lt;- temp$estimate[2] Results[i-2,5] &lt;- sd(Data[which(Data$Group == 2), i]) ## P value for difference Results[i-2,6] &lt;- temp$p.value rm(temp) } </code></pre> <p>Now we can make our results pretty and print them.</p> <pre><code>## Add column names names(Results) &lt;- c("Variable", "Group.1.Mean", "Group.1.SD", "Group.2.Mean", "Group.2.SD", "P.Value") ## View our results &gt; Results Variable Group.1.Mean Group.1.SD Group.2.Mean Group.2.SD P.Value 1 V1 0.21544390 0.9404104 -0.01426226 1.0570324 0.2537820 2 V2 0.26287585 1.0048291 0.22992285 0.9709686 0.8679038 3 V3 -0.06112963 0.9855287 0.17423440 1.0198694 0.2434507 4 V4 0.33848678 0.9360016 0.07905932 0.9106595 0.1632705 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload