I need to know if a person belong to a unique group or several groups and add a new column with boolean values that describe this condition.
Example data:
df <- structure(list(group = c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 2L,
1L, 3L), person = c(955563L, 955563L, 855563L, 855563L, 744506L,
744506L, 744506L, 444506L, 444506L, 555563L, 555563L)), .Names = c("group",
"person"), row.names = c(NA, -11L), class = "data.frame")
Result:
group person same_group
1 955563 TRUE
1 955563 TRUE
2 855563 TRUE
2 855563 TRUE
3 744506 TRUE
3 744506 TRUE
3 744506 TRUE
1 444506 FALSE
2 444506 FALSE
1 555563 FALSE
3 555563 FALSE
I think some window functions with dplyr can make it but I cannot figure out. Thanks in advance.
Try
A similar option using
data.table
is