[R] renaming factor-labels / add factors etc.
David Winsemius
dwinsemius at comcast.net
Tue Dec 16 04:32:33 CET 2008
On Dec 15, 2008, at 10:15 PM, Peter Alspach wrote:
> Jörg
>
> To add a level you could:
>
> x <- factor(c("xyz1", "abc1", "xyz2", "abc2"))
> x1 <- factor(x, levels=c(levels(x), 'fgh'))
Could also use:
levels(ff) <- c(levels(ff), "fgh")
>
> summary(x)
> abc1 abc2 xyz1 xyz2
> 1 1 1 1
> summary(x1)
> abc1 abc2 xyz1 xyz2 fgh
> 1 1 1 1 0
>
> For the others, I tend to convert to a character, make the changes
> and convert back to a factor - but there is probably a better way.
> levels(ff)[3] <- "zippy"
> levels(ff)
[1] "abc1" "abc2" "zippy" "xyz2" "fgh"
Just a note that these are not the names:
> names(ff)
NULL
Although you could add names.
--
David Winsemius
>
>
> HTH ....
>
> Peter Alspach.
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On Behalf Of Jörg Groß
>> Sent: Tuesday, 16 December 2008 3:26 p.m.
>> To: r-help at r-project.org
>> Subject: [R] renaming factor-labels / add factors etc.
>>
>> Hi,
>>
>> how can I change a defined factor-variable?
>> Like adding levels, renaming existing levels or merge several
>> levels of a factor to one level?
>>
>>
>>
>> For example; following factor-variable is given:
>>
>> x <- factor(c("xyz1", "abc1", "xyz2", "abc2"))
>>
>>
>> How can I add the level fgh?
>> And how can I merge "xyz1" and "xyz2" to one level?
>> And how can I change the name?
>>
>>
>>
>>
>> Thanks for any help!
>>
>> ______________________________________________
>> 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.
>>
>
> The contents of this e-mail are confidential and may be subject to
> legal privilege.
> If you are not the intended recipient you must not use, disseminate,
> distribute or
> reproduce all or any part of this e-mail or attachments. If you
> have received this
> e-mail in error, please notify the sender and delete all material
> pertaining to this
> e-mail. Any opinion or views expressed in this e-mail are those of
> the individual
> sender and may not represent those of The New Zealand Institute for
> Plant and
> Food Research Limited.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list