10

I wonder how to extract emmean and SE columns from emmGrid of emmeans R package. MWE is given below.

library(emmeans)
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
Test <- emmeans(warp.lm,  specs = "wool")

Test
wool   emmean       SE df lower.CL upper.CL
 A    31.03704 2.105459 48 26.80373 35.27035
 B    25.25926 2.105459 48 21.02595 29.49257

Results are averaged over the levels of: tension 
Confidence level used: 0.95 

class(Test)
[1] "emmGrid"
attr(,"package")
[1] "emmeans"
halfer
  • 19,824
  • 17
  • 99
  • 186
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • 1
    Hi MYaseen208. We tend to prefer succinct questions here, so if you can trim any non-essential material, that is appreciated by editors and readers. You've added notes about your future appreciation to around ~170 questions, and it's not necessary. Assume we know that you will (highly) appreciate help! Thanks. – halfer Jan 26 '18 at 17:12

1 Answers1

12

summary(Test) gives a data.frame instead.

class(summary(Test))
[1] "summary_emm" "data.frame" 

So one can do:

summary(Test)$emmean
[1] 31.03704 25.25926

And

summary(Test)$SE
[1] 2.105459 2.105459

To actually get a new subsetted data.frame, you need to explicitly coerce to class data.frame:

as.data.frame(summary(Test))[c('emmean', 'SE')]
    emmean       SE
1 31.03704 2.105459
2 25.25926 2.105459
Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 5
    Note emmeans 1.1 added an `as.data.frame` method. So `as.data.frame(Test)` will do the job. – Russ Lenth Jan 28 '18 at 14:04
  • 1
    Just a note: for pairwise comparisons, `summary(pairs(Test))$p.value` will give you the p-values. This is particularly useful when performing a large amount of pairwise comparisons as you can then simply extract p values inferior to 0.05 (or your favorite arbitrary significance threshold) with `which()`. – Nakx Nov 08 '18 at 01:19
  • Dear Axeman, apologies for pinging, do you know why in [this question](https://stackoverflow.com/questions/61903460/eff-size-from-package-emmeans-in-r-not-returning-effect-sizes) `emmeans` doesn't return any output? – rnorouzian May 20 '20 at 01:24
  • What about if you added a cld() statement and wanted to pull out group to make a table? So instead of Test <- emmeans(warp.lm, specs = "wool") it was Test <- cld(emmeans(warp.lm, specs = "wool")? summary(Test)$group does not work in this situation – yaynikkiprograms Oct 11 '21 at 12:40