[R] how to replace some objects?

Adrian Dusa dusa.adrian at gmail.com
Thu Dec 21 14:42:52 CET 2006


On Tuesday 19 December 2006 13:49, Michael Kubovy wrote:
> On Dec 19, 2006, at 3:05 AM, Zhang Jian wrote:
> > 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.
>
> let <- c('a', 'b', 'a', 'c', 'b', 'b', 'a', 'a', 'c')
> library(car)
> num <- recode(let, " 'a' = 1; 'b' = 2; else = 3 ")

Or, since the initial vector has letters only:
> as.numeric(factor(let))
[1] 1 2 1 3 2 2 1 1 3

Hth,
Adrian

-- 
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd
050025 Bucharest sector 5
Romania
Tel./Fax: +40 21 3126618 \
          +40 21 3120210 / int.101



More information about the R-help mailing list