Skip to content
Back to formatted view

Raw Message

Message-ID: <OF446C3FC5.F5C94666-ONC1257672.003AA170-C1257672.003BF131@precheza.cz>
Date: 2009-11-18T10:54:44Z
From: PIKAL Petr
Subject: Odp:  recode according to old levels
In-Reply-To: <4B03AF48.9010105@gmx.de>

Hi


r-help-bounces at r-project.org napsal dne 18.11.2009 09:24:40:

> Dear R-users,
> 
> i try to recode a factor according to old levels
> 
> F <- factor(sample(c(rep("A", 4), rep("B",2), rep("C",5))))
> 
> recode(F, "levels(F)[c(1,3)]='X'; else='Y'")
> 
> i tried to work with eval or expression around levels(F)[c(1,3)], but 
> nothing seems to work.
> 

I am not sure if recode does not want as an input a numeric vector. 
However for recoding levels of factor you can add a vector of new levels 
in correct order.

set.seed(111)
F <- factor(sample(c(rep("A", 4), rep("B",2), rep("C",5))))
levels(F)<-c("X","X","Y")
F
 [1] Y Y X X X Y X X X Y Y
Levels: X Y

or maybe

levels(F)<- ifelse(levels(F) %in% c("A","B"), "X", "Y")

Regards
Petr


> 
> Many thanks if anyone could tell me what i've missed and what's the 
> problem here.
> 
> best regards
> 
> Andreas
> 
> ______________________________________________
> 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.