[R] create new column by replacing multiple unique values in existing column

raz barvazduck at gmail.com
Thu Sep 11 17:49:02 CEST 2014


Hi,

​I got the following data frame:
 dat1 <- read.table(text="a,b
1,A1
2,A1
3,A1
4,A1
5,A1
6,A2
7,A2
8,A2
9,A2
10,A2
11,B1
12,B1
13,B1
14,B1
15,B1",sep=",",header=T)
​

​I would like to add a new column dat1$new based on column "b" (dat$b) in
which values will be substituted according to their unique values e.g "A1"
will be "1", "A2" will be "2" and so on (this is only a part of a large
table). It would be better if I could change all unique values in dat1 to
numbers 1:unique(n). if not then how do I change all values
("A1","A2","B1") to (1,2,3) in a new column?.

Thanks a lot,

Raz​


-- 
\m/

	[[alternative HTML version deleted]]



More information about the R-help mailing list