0

I have a large dataset on which I am performing ANOVA analysis. I'm not sure how to get the output of the analysis into a table that I can use in a Word document (without retyping all of the values manually).

Here is an example of what I'm trying to do:

var1 <- c("Red", "Green", "Blue", "Blue", "Red","Red", "Green", "Blue",    "Blue", "Red",
      "Red", "Blue", "Green", "Blue", "Red","Red", "Green", "Blue", "Blue", "Red")
var2 <- c(10, 20, 15, 32, 10, 20, 15, 32, 10, 20, 15, 32, 10, 20, 15, 32, 10, 20, 15, 32)

df <- data.frame(var1, var2)

TukeyHSD(aov(var2 ~ var1))

This produces an output that looks like this:

  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = var2 ~ var1)

$var1
            diff        lwr       upr     p adj 
Green-Blue -8.25 -21.183389  4.683389 0.2580147 
Red-Blue   -2.75 -13.310068  7.810068 0.7848043 
Red-Green   5.50  -7.433389 18.433389 0.5323260

I would like the output to be in a format that is easy to cut and paste into Word that includes the headings, "Variable", "Difference" and "p value". Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Greg Martin
  • 243
  • 3
  • 5
  • 17
  • Save output `output <- TukeyHSD(aov(var2 ~ var1))` and `output$var1` will contain a matrix with your values. You can convert to data frame with `as.data.frame(output$var1)` – CPak Jul 20 '17 at 16:31
  • Thanks @ChiPak . That solution does help but I still don't have a table that is "publication ready" (if that makes sense). I can't just cut and paste the output into Word without it looking a little messy. – Greg Martin Jul 20 '17 at 16:47
  • 1
    Possible duplicate of [General guide for creating publication quality tables using R, Sweave, and LaTeX](https://stackoverflow.com/questions/9660359/general-guide-for-creating-publication-quality-tables-using-r-sweave-and-latex) – Hack-R Jul 20 '17 at 16:49
  • `write.tsv(as.data.frame(output$var1), "C:\folder", header=T)` and open with Excel. – CPak Jul 20 '17 at 16:51
  • thanks @ChiPak - I get an error saying "could not find function "write.tsv"" (is there a package that I need to install to access that function?) – Greg Martin Jul 20 '17 at 17:17
  • Sorry, I normally use `readr` which has `write_tsv`. I think you have to use `write.csv(as.data.frame(output$var1), "C:\folder", header=T, sep="\t")` or you can not specify `sep` for a comma-delimited file. – CPak Jul 20 '17 at 18:45

0 Answers0