我有一个tbl_df,我希望 group_by(u, v)
用 (u, v)
观察到的每个不同的整数组合 .
EDIT: 这是通过在dplyr 0.4.0中添加 group_indices()
来解决的
a)然后我想为每个不同的组分配一些任意的不同数字标签= 1,2,3 ...组合(u,v)==(2,3)可以得到标签1,(1,3)可以得到2,依此类推 . 如何使用一个 mutate()
,没有三步汇总和自我加入?
dplyr有一个简洁的函数 n()
,但是它给出了组的数量 within ,而不是整个 number of the group . In data.table this would simply be called .GRP .
b)实际上我真正想要分配一个字符串/字符标签('A','B',...) . 但是按整数对组进行编号是足够好的,因为我可以使用 integer_to_label(i)
,如下所示 . 除非有这个部分出汗 .
set.seed(1234)
# Helper fn for mapping integer 1..26 to character label
integer_to_label <- function(i) { substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",i,i) }
df <- tbl_df(data.frame(u=sample.int(3,10,replace=T), v=sample.int(4,10,replace=T)))
# Want to label/number each distinct group of unique (u,v) combinations
df %>% group_by(u,v) %>% mutate(label = n()) # WRONG: n() is number of element within its group, not overall number of group
u v
1 2 3
2 1 3
3 1 2
4 2 3
5 1 2
6 3 3
7 1 3
8 1 2
9 3 1
10 3 4
KLUDGE 1: could do df %>% group_by(u,v) %>% summarize(label = n()) , then self-join
5 回答
更新的答案
您还可以考虑以下稍微不可读的版本
使用
iterators
包dplyr有一个
group_indices()
函数,您可以像这样使用:使用
data.table
的另一种方法是这导致:
用三种不同的方式更新我的答案:
A)使用
interaction(u,v)
的整洁的非dplyr解决方案:B)使Randy的快速和肮脏的发电机功能更加紧凑:
C)这里还有一个使用生成器函数的单行程序滥用this的全局变量赋值:
我没有足够的评论声誉,所以我发布了一个答案 .
使用factor()的解决方案是一个很好的解决方案,但它的缺点是在factor()按字母顺序排列其级别后分配了组号 . dplyr的group_indices()也会发生同样的行为 . 您可能希望根据当前组顺序将组编号从1分配给n . 在这种情况下,您可以使用: