Re: [R] Changing values (factors) does not change levels of that value?!

From: Weiss, Bernd <bernd.weiss_at_uni-koeln.de>
Date: Sun, 16 Nov 2008 14:44:12 +0100

Philipp Pagel schrieb:

>>  * when then looking at str(weblog),
>>    the "-" will stay in the levels, mentioned for the variable weblog$V8
>>    -> BAD!
>>
>> Is this snormal behaviour?

>
> Yes, it is. The idea is that a factor has a given set of levels
> independent of how often you find them in your data - including
> the case that a level is not observed at all. E.g. gender cn take
> levels 'male' or 'female' but you may have a sample of females.
>
>> Do I have to throw out the unwanted level by myself?

>
> Yes, and it's easy:
>
>> x <- factor(c('A','B','C','A','C'))
>> y <- x[x!='C']
>> y

> [1] A B A
> Levels: A B C
>> factor(y)

> [1] A B A
> Levels: A B
>

another solution might be

 > x <- factor(c('A','B','C','A','C'))
 > y <- x[x!='C']
 > y

[1] A B A
Levels: A B C
 > y[drop = TRUE]
[1] A B A
Levels: A B

HTH, Bernd



R-help_at_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. Received on Sun 16 Nov 2008 - 13:47:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 16 Nov 2008 - 14:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive