AW: [R] constructing specially ordered factor

Petr Pikal petr.pikal at precheza.cz
Mon Oct 4 16:37:20 CEST 2004



On 4 Oct 2004 at 15:27, Khamenia, Valery wrote:

> Hi Petr,
> 
> Thank you for your reply.
> 
> > Factor <- function(f,n, decreasing=TRUE, ...) {
> > ooo<-order(levels(factor(n)), decreasing=decreasing)
> > my.order<-levels(factor(f))[ooo]
> > factor(f, levels=my.order)
> > }
> 
> it works incorrectly. Indeed, let's apply with your Factor:
> 
>   unames <- c("thousands", "units", "dozens", "hundreds", "thousands",
> "dozens")
>   u <- c(1000, 1,10,100, 1000, 10)
>   Factor(unames, u)
> 
> the above produces the following output:
> 
>   [1] thousands units     dozens    hundreds  thousands dozens  
>   Levels: units thousands hundreds dozens
> 
> where "dozens" > "hundreds"

Hi

So I obviously used incorect input (or an input which worked with 
my first attempt :-). 

I am not sure if this one is any better but it works with unames and 
u correctly (I hope). 

Factor <- function(f, n, ...) {

ooo<-order(u)
new.ord.unames<-unames[ooo]
factor(f, levels=unique(new.ord.unames))

}


Cheers
Petr

> 
> ---
> Valery

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list