Non-partitioning group-?

17 views
Skip to first unread message

Marcus Walz

unread,
Jun 30, 2016, 1:21:15 PM6/30/16
to manipulatr
Say I have a data frame like this:

X  Y
1  A
2  B
3  C
4  D
5  E
6  F


And I want to group it like this by X:

X  Y
1  A (Group 1)
2  B
----
1  A (Group 2)
2  B
3  C
----
2  B (Group 3)
3  C
4  D
----
3  C (Group 4)
4  D
5  E
----
4  D (Group 5)
5  E
6  F
----
5  E (Group 6)
6  F

We can achieve this effect using a join:

grouping <- dat %>% select(X) %>% rowwise %>% do(data_frame(Group=.$X, X = (.$X-1):(.$X+1)))
left_join
(grouping, dat) %>% group_by(Group)
 
But this becomes increasing complicated, if we're mixing in other groups, and the complexity and intermediate memory this method requires becomes quite large. Is there anyway to generalize this? It looks like dtplyr's groupings assume a many-to-one relationship.

Thanks,

Reply all
Reply to author
Forward
0 new messages