Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbering of groups in dplyr?

Tags:

r

group-by

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.

like image 718
Alexander Avatar asked Jan 19 '26 15:01

Alexander


1 Answers

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
..  ... .   ...
like image 89
N311V Avatar answered Jan 21 '26 07:01

N311V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!