如何通过group_by中的group-number对数据表进行编号/标记?

我有一个tbl_df我想要group_by(u, v)观察的每个不同的整数组合(u, v)。


编辑:通过添加group_indices()回dplyr 0.4.0解决了这个问题


a)然后我想为每个不同的组分配一些任意的不同数字标签= 1,2,3 ......例如组合(u,v)==(2,3)可以得到标签1,(1,3)可以得到2,依此类推。如何使用一个mutate(),没有三步总结和自我加入?


dplyr有一个整洁的功能n(),但是,让元素的数目之内它的组,而不是整体的组的数目。在data.table这将简单地被称为.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


鸿蒙传说
浏览 634回答 3
3回答

慕丝7291255

更新的答案get_group_number = function(){&nbsp; &nbsp; i = 0&nbsp; &nbsp; function(){&nbsp; &nbsp; &nbsp; &nbsp; i <<- i+1&nbsp; &nbsp; &nbsp; &nbsp; i&nbsp; &nbsp; }}group_number = get_group_number()df %>% group_by(u,v) %>% mutate(label = group_number())您还可以考虑以下稍微不可读的版本group_number = (function(){i = 0; function() i <<- i+1 })()df %>% group_by(u,v) %>% mutate(label = group_number())使用iterators包library(iterators)counter = icount()df %>% group_by(u,v) %>% mutate(label = nextElem(counter))

Helenr

dplyr有一个group_indices()你可以使用的功能:df %>%&nbsp;&nbsp; &nbsp; mutate(label = group_indices(., u, v)) %>%&nbsp;&nbsp; &nbsp; group_by(label) ...

慕田峪4524236

使用的另一种方法data.table是require(data.table)setDT(df)[,label:=.GRP, by = c("u", "v")]这导致:&nbsp; &nbsp; u v label&nbsp;1: 2 1&nbsp; &nbsp; &nbsp;1&nbsp;2: 1 3&nbsp; &nbsp; &nbsp;2&nbsp;3: 2 1&nbsp; &nbsp; &nbsp;1&nbsp;4: 3 4&nbsp; &nbsp; &nbsp;3&nbsp;5: 3 1&nbsp; &nbsp; &nbsp;4&nbsp;6: 1 1&nbsp; &nbsp; &nbsp;5&nbsp;7: 3 2&nbsp; &nbsp; &nbsp;6&nbsp;8: 2 3&nbsp; &nbsp; &nbsp;7&nbsp;9: 3 2&nbsp; &nbsp; &nbsp;610: 3 4&nbsp; &nbsp; &nbsp;3
打开App,查看更多内容
随时随地看视频慕课网APP