Re: [R] Tables without names

2009-06-12 Thread Duncan Murdoch

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
11 1
22 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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Tables without names

2009-06-12 Thread Martin Maechler
 DM == Duncan Murdoch murd...@stats.uwo.ca
 on Fri, 12 Jun 2009 06:09:14 -0400 writes:

DM 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:

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

indeed!

 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?

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

many months ago in a discussion about the use (and misuse) of 
c() for coercing arrays/matrices to (atomic) vectors, Brian
Ripley  I think  advertized the use of as.vector() 
with which I strongly agree.

as.vector() here has the additional advantage of *not*
transforming integer into double.

Martin Maechler, ETH Zurich

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Tables without names

2009-06-12 Thread Stavros Macrakis
On Fri, Jun 12, 2009 at 6:09 AM, Duncan Murdoch murd...@stats.uwo.cawrote:

 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?

   -s

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Tables without names

2009-06-12 Thread Gabor Grothendieck
On Fri, Jun 12, 2009 at 10:57 AM, Stavros Macrakismacra...@alum.mit.edu wrote:
 On Fri, Jun 12, 2009 at 6:09 AM, Duncan Murdoch murd...@stats.uwo.cawrote:

 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.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Tables without names

2009-06-11 Thread Stavros Macrakis
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
11 1
22 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?

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Tables without names

2009-06-11 Thread David Winsemius

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
   11 1
   22 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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.