1

I want to add a numeric column "ID" each each unique 'cat' value.. In other teams, enumerate "cat"

    cat        val  
1  aaa 0.05638315  
2  aaa 0.25767250  
3  aaa 0.30776611  
4  aaa 0.46854928  
5  aaa 0.55232243  
6  bbb 0.17026205  
7  bbb 0.37032054  
8  bbb 0.48377074  
9  bbb 0.54655860  
10 bbb 0.81240262  
11 ccc 0.28035384  
12 ccc 0.39848790  
13 ccc 0.62499648  
14 ccc 0.76255108  
15 ccc 0.88216552 

Intended output with a numeric ID column

df  
   cat        val    ID
1  aaa 0.05638315    1
2  aaa 0.25767250    1
3  aaa 0.30776611    1
4  aaa 0.46854928    1
5  aaa 0.55232243    1
6  bbb 0.17026205    2
7  bbb 0.37032054    2
8  bbb 0.48377074    2
9  bbb 0.54655860    2
10 bbb 0.81240262    2
11 ccc 0.28035384    3
12 ccc 0.39848790    3
13 ccc 0.62499648    3
14 ccc 0.76255108    3
15 ccc 0.88216552    3

2 Answers2

0

df$ID <- sapply(df$cat, function(x) which(x == unique(df$cat)))

Gwang-Jin Kim
  • 9,303
  • 17
  • 30
0

df$ID <- as.numeric(factor(df$cat, levels=unique(df$cat)))

or

factor(v, levels=c("a", "c", "b"), labels=seq(unique(v)))

levels=unique(df$cat) ensures numbering by order of appearance 1, 2, 3 ... whenfactor()-ising the vector df$cat. as.numeric() shows the numbers underlying the factors.

df$ID <- unlist(mapply(rep, seq(unique(df$cat)), table(df$cat)))

Gwang-Jin Kim
  • 9,303
  • 17
  • 30