4

I'm having trouble exporting my TukeyHSD results so that they are separated in cells when I open the results up in something like Excel. I tried using write.csv() but it says:

cannot coerce class "c("TukeyHSD", "multicomp")" to a data.frame

How can I capture my TukeyUSD results in a way that I can just copy and paste them into an Excel sheet?

Ren Zhang
  • 91
  • 1
  • 2
  • 7
  • Can you please edit to make this a [reproducible question](http://stackoverflow.com/help/mcve)? I think it's fantastic that you are participating in the SO community, you need to take a little time to learn how to be a good SO citizen. This is arguably the third non-reproducible question you have posted in a week. Also, if people provide an answer that solves your problem, please take the time to click the check box next to their answer (accepting the answer). – dayne Jul 25 '16 at 15:28
  • I'll be happy to remove the down-vote once you edit the question. See in my answer how I provided a complete example to make the code run and illustrate problem. Often, if you're looking for a quick example you can go to the help file for that function. In this case I lifted the example from `?TukeyHSD`. – dayne Jul 25 '16 at 15:40
  • I figured it out but I will be sure to follow your advice for next time that I need help. Thank you! – Ren Zhang Jul 25 '16 at 15:52

2 Answers2

9

TukeyHSD returns a an object of class "TukeyHSD". You can extract the table of results from the "TukeyHSD" object using the $ operator. You can then export or modify the table in any way you see fit.

fm1 <- aov(breaks ~ wool + tension, data = warpbreaks)
res <- TukeyHSD(fm1, "tension", ordered = TRUE)
as.data.frame(res$tension)
#          diff        lwr      upr       p adj
# M-H  4.722222 -4.6311985 14.07564 0.447421021
# L-H 14.722222  5.3688015 24.07564 0.001121788
# L-M 10.000000  0.6465793 19.35342 0.033626219
dayne
  • 7,504
  • 6
  • 38
  • 56
5

This one worked for me

ANOVA_Tc<-aov(Concentration~ Sample, data= Tc)

summary(ANOVA_Tc)

TKHSD_Tc <- TukeyHSD(ANOVA_Tc)

TK<-(TKHSD_Tc)

TK_data<-as.data.frame(TK[1]) # the [1] locates the part of the output to be exported

write.csv(TK_data, 'TK_data.csv')
Abdessabour Mtk
  • 3,895
  • 2
  • 14
  • 21
Shakes
  • 51
  • 1
  • 1