0

When extracting elements from a named list, R automatically drops the names when the list is of length 1. Is it possible to keep the names?

Exemple:

Code:R

lin_mod = lm(data, CompStength~ .)
anova(lin_model)

anova(lin_model)[,"Sum Sq"] does not keep the names (e.g. CA.TA, TA.C, ...) but returns

     [1]   86.103991 2331.962769  932.229151 1415.558985  457.608362 

I would like to get:

                 Sum Sq 
CA.TA             86.1   
TA.C            2332.0  
W.C              932.2  

Just as when I run anova(lin_model)[,c("Sum Sq","Df")] I get:

               Sum Sq   Df
CA.TA             86.1  3
TA.C            2332.0  3
W.C              932.2  2
Temp            1415.6  2
Malo Marrec
  • 599
  • 1
  • 6
  • 20
  • 2
    I'm trying to find a `drop = FALSE` duplicate or canonical question now...it's also listed in most official R FAQs. – joran Feb 08 '17 at 22:39
  • [This](http://stackoverflow.com/q/12755397/324364) is a possibility, but there may be better ones... – joran Feb 08 '17 at 22:40
  • `anova(lin_model)[, "Sum Sq", drop = FALSE]`? – Phil Feb 08 '17 at 22:41
  • Also covered in the SO R-Docs in the "basic subsetting/data frame" section: http://stackoverflow.com/documentation/r/1686/basic-subsetting/5447/data-frames#t=201702082311161733888 – Gregor Thomas Feb 08 '17 at 23:14

0 Answers0