[R] Tables without names

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 12 17:23:45 CEST 2009


On Fri, Jun 12, 2009 at 10:57 AM, Stavros Macrakis<macrakis at alum.mit.edu> wrote:
> On Fri, Jun 12, 2009 at 6:09 AM, Duncan Murdoch <murdoch at stats.uwo.ca>wrote:
>
>> 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.
>
>
> Yes, that is one of its flaws.  More specifically, in this case, operations
> on S3 objects can change them from being valid to being invalid.
>
>
>> I think that's all you're seeing here:  you've got something that is
>> mislabelled as being of class "table".
>
>
> Yes.
>
>
>> The solution is "don't do that".
>
>
> Agreed!  But it's not clear to me how unname can *know* how not to do that
> in the general case.  After all, unname on a vector of POSIXct's leaves a
> valid POSIXct object.
>
> ...
>>> 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.
>>
>
> Makes sense, as does the suggestion as.vector.  So I guess the summary of
> 'stripping' operations is:
>
> c  --- strip all attributes (including most but not all classes) except for
> names
> unname -- strip name attributes, but no other attributes (including class)
> unclass -- strip only class attribute
> as.vector -- strip all attributes including class and name; convert generic
> vectors to atomic vectors
>
> Am I missing others?

There is also unlist.

Also c and unlist are generic so their action can depend on the class
of their argument.




More information about the R-help mailing list