[R] converting numeric to ordered

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 14 10:23:27 CEST 2001


On Fri, 14 Sep 2001, Duncan Mackay wrote:

> Hello all,
> would someone please tell me why the following code doesn't "work" (i.e. do
> what i expected!).
> I wanted to convert numeric variables in a matrix to ordered (for input as
> nominal variables into the 'daisy' program).
> Why does the following code seem to work, but the "is.ordered" command
> reports that the variables are not ordered (factors)??

It's a matrix. "ordered" is a class. Only data frames can have columns of
different classes.  I think you want

> xxx <- as.data.frame(xxx)
> xxx
  V1 V2 V3
1  0  1  0
2  1  0  1
3  0  1  0

xxx[] <- lapply(xxx, as.ordered)

When ?daisy says

       x: data matrix or dataframe.  Dissimilarities will be computed
          between the rows of `x'.  Columns of mode `numeric' will be
          recognized as interval scaled variables, columns of class
          `factor' will be recognized as nominal variables, and columns
          of class `ordered' will be recognized as ordinal variables.

the comment applies to data frames only.  (Martin: might you clarify
this?)

> > xxx <- matrix(c(0,1,0,1,0,1,0,1,0),ncol=3)
> > xxx
>      [,1] [,2] [,3]
> [1,]    0    1    0
> [2,]    1    0    1
> [3,]    0    1    0
> > yyy <- apply(xxx,2,as.ordered)
> > yyy
>      [,1] [,2] [,3]
> [1,]    1    2    1
> [2,]    2    1    2
> [3,]    1    2    1
> > apply(yyy,2,is.ordered)
> [1] FALSE FALSE FALSE
>
> Duncan
>
> *******************************************
> Dr. Duncan Mackay
> Biology
> Flinders University
> GPO Box 2100
> Adelaide
> S.A.    5001
> AUSTRALIA
>
> Ph (08) 8201 2627    FAX (08) 8201 3015
>
> http://www.bio.flinders.edu.au/dam/damres.htm
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list