[R] how to replace some objects?

Jacques VESLOT jacques.veslot at good.ibl.fr
Tue Dec 19 09:27:40 CET 2006


you can use as.numeric(factor( )); in your example:
 > ex <- sample(letters[1:3], 10, T)
 > ex
  [1] "b" "b" "c" "b" "a" "a" "b" "b" "a" "a"

 > as.numeric(factor(ex))
  [1] 2 2 3 2 1 1 2 2 1 1

if the order is different, use levels:

 > as.numeric(factor(ex, levels=letters[3:1]))
  [1] 2 2 1 2 3 3 2 2 3 3

-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------

Zhang Jian a écrit :
> I want to replace some objects in one row or column.For example,
> One colume: a,b,a,c,b,b,a,a,c.
> I want to replace "a" with "1", "b" with "2", and "c" with "3".
> Like this: 1,2,1,3,2,2,1,1,3.
> 
> How to do it? I donot know how to do it. Thanks.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list