Re: [R] Question about creating unique factor labels with the factor function

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Wed 20 Jul 2005 - 19:55:07 EST


Hallo

I am not sure what you want to achieve.

your factor has 3 levels but with only 2 different labels

> hb

[1] 1 1 1 1 1 1 2 2 2
Levels: 1 1 2

but

> str(hb)

 Factor w/ 3 levels "1","1","2": 1 1 1 2 2 2 3 3 3

so you gave only one label to level 1 and 2. You can give the same label to any level you choose but I can not see a reason in it.

> hb <- factor(c(1,1,1,2,2,2,3,3,3),

levels=c(1,2,3),labels=c(1,1,1))
> hb

[1] 1 1 1 1 1 1 1 1 1
Levels: 1 1 1

or

> hb <- factor(c(1,1,1,2,2,2,3,3,3),

levels=c(1,2,3),labels=c(3,100,100))
> hb

[1] 3 3 3 100 100 100 100 100 100
Levels: 3 100 100
> str(hb)

 Factor w/ 3 levels "3","100","100": 1 1 1 2 2 2 3 3 3



but surprising for me is an ordered behaviour which I find a bit odd.

> hb

[1] 3 3 3 100 100 100 100 100 100
Levels: 3 100 100

> ordered(hb)

[1] 3 3 3 100 100 100 100 100 100
Levels: 3 < 100 < 100

> str(hb)

 Factor w/ 3 levels "3","100","100": 1 1 1 2 2 2 3 3 3

> str(ordered(hb))

 Ord.factor w/ 3 levels "3"<"100"<"100": 1 1 1 2 2 2 2 2 2

> unique((hb))

[1] 3 100 100 #three levels and three values Levels: 3 100 100

> unique(ordered(hb))
[1] 3 100 # three levels but only 2 values? Levels: 3 < 100 < 100



Can anybody explain why unique(ordered()) results in only 2 displayed levels although this

> str(unique(ordered(hb)))
 Ord.factor w/ 3 levels "3"<"100"<"100": 1 2

says that the factor still have 3 levels?

Best regards
Petr

On 19 Jul 2005 at 15:11, Gregory Gentlemen wrote:

> Hi guys,
>
> I ran into a problem of not being able to create unique labels when
> creating a factor. Consider an example below:
>
> hb <- factor(c(1,1,1,2,2,2,3,3,3), levels=c(1,2,3),labels=c(1,1,2)) >
> hb [1] 1 1 1 1 1 1 2 2 2 Levels: 1 1 2
> unique(hb) [1] 1 1 2 Levels:
> 1 1 2
>
> How come there are three unique levels, I thought this would only
> create one unique level?
>
> > unique(as.ordered(hb))
> [1] 1 2
> Levels: 1 < 1 < 2
>
> Is as.ordered the only solution?
>
> Thanks in advance,
> Greg
>
>
> __________________________________________________
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal@precheza.cz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jul 20 20:00:56 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:51 EST