Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Why doesn't my code work for plotting a box-plot and subsequent t-test?

$
0
0

Here is my data (df called shannon_boxplot);

        Samples   Diversity

   1    Control 3.309361
   2    Control 3.664494
   3    Control 3.269842
   4    Disease 2.572888
   5    Disease 1.530877
   6    Disease 2.357401

 dput(shannon_boxplot)

 structure(list(Samples = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = 
 c("Control", 
 "Disease"), class = "factor"), Diversity = structure(c(5L, 6L, 
  4L, 3L, 1L, 2L), .Label = c("1.530877", "2.357401", "2.572888", 
  "3.269842", "3.309361", "3.664494"), class = "factor")), class = 
  "data.frame", row.names = c(NA, 
  -6L))

I am trying to plot a box plot to compare disease vs control. Here is the code that I have tried;

       boxplot(Diversity ~ Samples, data=shannon_boxplot, xlab="Samples",
       ylab="Shannon Diversity")

I am met with this error message Error in oldClass(stats) <- cl : adding class "factor" to an invalid object.

Using 1 for Controls and 2 for Disease in the sample column, gets the same error.

When I try and carry out a t test to compare the means of each group, I use this data;

                shannon_diversity
    Control1    3.309361
    Control2    3.664494
    Control3    3.269842
    Disease1    2.572888
    Disease2    1.530877
    Disease3    2.357401

   control_shannon <- shannon_divplot[1:3,1]
   disease_shannon <- shannon_divplot[4:6,1]

   ttest_out <- t.test(disease_shannon, control_shannon, alternative="two.sided", paired=F)$pvalue

I get the error message NULL with the above. Any help would be greatly appreciated. Thank-you


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>