[R] help

Bill Venables venables at acland.qld.cmis.csiro.au
Thu Jun 22 04:17:40 CEST 2000

Kong, Chuang Fong asks:

> Dear All,
> Further to answers to sorting, can someone tell me how can I sort a list
> that contains duplicates (name) but keeping the duplicates together when
> sorting the values. 
> e.g., a data frame with name (in duplicates), Mvalues(may be different for
> the duplicates)
> name	M
> 1234	8
> 1234	8.3
> 4321	9
> 4321	8.1
> If I sort by M, it will be 1234, 4321, 1234, 4321 but I want to keep the
> duplicates together, so that it will give me 1234, 1234, 4321, 4321. I also
> would like to set a cut-off, so that anything below a certain values will
> not be sorted. Any help will be appreciated.

Doesn't this mean you really want to sort on name and on M within
name? As several people have pointed out this is precisely what
order() is designed to do.

I take it that the cutoff is on the value of M.  OK, suppose it is the
value of `coff'.

sort.ind <- order(name, pmax(coff, M))  #  sorting index
name <- name[sort.ind]
M <- M[sort.ind]

Notice how using pmax() for "parallel maximum" you can implement the
cutoff by raising all values below the mark up to the mark thus
putting them all into the same bin as far as sorting is concerned. 

If your two variables are in a data frame you can combine the last two
steps into one, of course.

sort.ind <- order(dat$name, pmax(coff, dat$M))
dat <- dat[sort.ind, ]

In fact it's not long before you are doing it all in one step:

dat <- dat[order(dat$name, pmax(coff, dat$M)), ]

Bill Venables,      Statistician,     CMIS Environmetrics Project
CSIRO Marine Labs, PO Box 120, Cleveland, Qld,  AUSTRALIA.   4163
Tel: +61 7 3826 7251           Email: Bill.Venables at cmis.csiro.au    
Fax: +61 7 3826 7304      http://www.cmis.csiro.au/bill.venables/

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