An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061219/041f18cd/attachment.pl
how to replace some objects?
6 messages · Jian Zhang, Jacques VESLOT, Michael Kubovy +1 more
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.
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061219/f4630b56/attachment.pl
similarly:
as.character(factor(c("abca","coma"),labels=c("aaa","bbb")))
-------------------------------------------------------------------
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 :
The example is simply.But the other example:
One column: acba,coma,acmo,acmo,acba,coma
I want to replace "acba" with "aaa", "coma" with "bbb", and "acmo" with
"ddd".
Like this: aaa,bbb,ddd,ddd,aaa,bbb
How to do it?
Thanks.
On 12/19/06, *Jacques VESLOT* <jacques.veslot at good.ibl.fr
<mailto:jacques.veslot at good.ibl.fr>> wrote:
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 <mailto: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. >
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 ")
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS: P.O.Box 400400 Charlottesville, VA 22904-4400
Parcels: Room 102 Gilmer Hall
McCormick Road Charlottesville, VA 22903
Office: B011 +1-434-982-4729
Lab: B019 +1-434-982-4751
Fax: +1-434-982-4766
WWW: http://www.people.virginia.edu/~mk9y/
2 days later
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