[R] Tables without names

David Winsemius dwinsemius at comcast.net
Fri Jun 12 03:30:55 CEST 2009


On Jun 11, 2009, at 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:
>
> 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 think of R (contingency) tables as just integer arrays with  
attitude  ... er, attributes.

 > tt <- table(c(1,1,1,3,3,3,5,5))
 > is.array(tt)
[1] TRUE

 > is.matrix( with(warpbreaks, table(wool, tension)) )
[1] TRUE

So what would you do with a matrix that had row or column names?

> --

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list