Alexander
Alexander

Reputation: 4635

Numbering of groups in dplyr?

I have question about numbering the groups in a data.frame.

I found only one similar approach here dplyr-how-to-number-label-data-table-by-group-number-from-group-by

but it didnt worked to me. I dont know why.

S <- rep(letters[1:12],each=6)
R = sort(replicate(9, sample(5000:6000,4)))
df <- data.frame(R,S)

get_next_integer = function(){
  i = 0
  function(S){ i <<- i+1 }
}
get_integer = get_next_integer() 

result <- df %>% group_by(S) %>% mutate(label = get_integer())
result

Source: local data frame [72 x 3]
Groups: S [12]

       R      S label
   (int) (fctr) (dbl)
1   5058      a     1
2   5121      a     1
3   5129      a     1
4   5143      a     1
5   5202      a     1
6   5213      a     1
7   5239      b     1
8   5245      b     1
9   5269      b     1
10  5324      b     1
..   ...    ...   ...

I look for elegant solution in dplyr. Numbering each letters from 1 to 12 etc.

Upvotes: 2

Views: 3048

Answers (2)

N311V
N311V

Reputation: 196

Using as.numeric will do the trick.

S <- rep(letters[1:12],each=6)
R = sort(replicate(9, sample(5000:6000,4)))
df <- data.frame(R,S)

result <- df %>% mutate(label = as.numeric(S)) %>% group_by(S)

result
Source: local data frame [72 x 3]
Groups: S

      R S label
1  5018 a     1
2  5042 a     1
3  5055 a     1
4  5066 a     1
5  5081 a     1
6  5133 a     1
7  5149 b     2
8  5191 b     2
9  5197 b     2
10 5248 b     2
..  ... .   ...

Upvotes: 6

Ven Yao
Ven Yao

Reputation: 3710

No need to use dplyr at all.

S <- rep(letters[1:12],each=6)
R = sort(replicate(9, sample(5000:6000,4)))
df <- data.frame(R,S)

df$label <- as.numeric(factor(df$S))

Upvotes: 4

Related Questions