Skip to content
Prev 35669 / 63424 Next

Inconsistency in as.data.frame.table for stringsAsFactors

Stavros Macrakis wrote:
"A foolish consistency is the hobgoblin of little minds..."

The thing is that if you are converting the classifying factors of a 
table to columns of a data frame, you will presumably prefer that they 
come out as factors, retaining level order. The alternative is like this:

 >  (x <- as.table(c("Rare"=5, "Medium"=2, "Well-done"=6)))
      Rare    Medium Well-done
         5         2         6
 > df <- as.data.frame(x, stringsAsFactors=F)
 > xtabs(Freq~Var1, data=df)
Var1
    Medium      Rare Well-done
         2         5         6

This is completely different from other cases, where as.data.frame will 
auto-convert character variables to factors; e.g., on reading. Having a 
global option intended for read.table() interfere with the above kind of 
operation, could be a really nasty surprise for the user. (Notice also 
that the option was introduced in 2.10.0, before then, noone would 
expect that classifying factors could come out as non-factors. 
Defaulting to the global option could easily break working code.)