在dplyr中编组的编号? [英] Numbering of groups in dplyr?
本文介绍了在dplyr中编组的编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里找到了一个类似的方法 dplyr-如何转号码标签数据表逐基团 - 来自群组的数量
但它对我没有帮助。我不知道为什么。
S < - rep(字母[1:12],each = 6)
(R,S)
get_next_integer = function(){
i = 0
函数(S){i <-i + 1}
}
get_integer = get_next_integer()
结果< - df %>%group_by(S)%>%mutate(label = get_integer())
结果
来源:本地数据框[72 x 3]
组:S [12]
RS标签
(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
.. ... ... ...
我在 dplyr
中寻找优雅的解决方案。编号从1到12等每个字母。
解决方案
使用as.numeric将会执行此操作。
$ $ p $
S < - rep(字母[1:12],each = 6)
R = sort(replicate(9,sample(5000: 6000)))
df< - data.frame(R,S)
结果< - df%>%mutate(label = as.numeric(S)) %>%group_by(S)
结果
来源:本地数据框[72 x 3]
组:S
RS标签
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
.. ...。 ...
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.
解决方案
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
.. ... . ...
这篇关于在dplyr中编组的编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文