[R] Tables without names

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jun 12 12:09:14 CEST 2009


On 11/06/2009 5:35 PM, Stavros Macrakis wrote:
> A table without names displays like a vector:
> 
>     > unname(table(2:3))
>     [1] 1 1 1
> 
> and preserves the table class (as with unname in general):
> 
>     > dput(unname(table(2:3)))
>     structure(c(1L, 1L), .Dim = 2L, class = "table")
> 
> Does that make sense?  R is not consistent in its treatment of such unname'd
> tables:

One of the complaints about the S3 object system is that anything can 
claim to be of class "foo", even if it doesn't have the right structure 
so that foo methods work for it.  I think that's all you're seeing here: 
  you've got something that is mislabelled as being of class "table". 
The solution is "don't do that".

  > In plot, they are considered erroneous input:
> 
>     > plot(unname(table(2:3)))
>     Error in xy.coords(x, y, xlabel, ylabel, log) :
>     'x' and 'y' lengths differ
> 
> but in melt, they act as though they have names 1:n:
> 
>    > melt(unname(table(2:3)))
>          indicies value
>     1        1     1
>     2        2     1
> 
> (By the way, is the spelling error built into too much code to be
> corrected?)
> 
>             -s
> 
> PS What is the standard way of extracting just the underlying vector?
> c(unname(...)) works -- is that what is recommended?

I would use as.numeric(), but I don't claim it's standard.

Duncan Murdoch




More information about the R-help mailing list