I have a data frame which shows membership in three color classes. Numbers refer to unique IDs. One ID may be a part of one group or multiple groups.
dat <- data.frame(BLUE = c(1, 2, 3, 4, 6, NA),
RED = c(2, 3, 6, 7, 9, 13),
GREEN = c(4, 6, 8, 9, 10, 11))
or for visual reference:
BLUE RED GREEN
1 2 4
2 3 6
3 6 8
4 7 9
6 9 10
NA 13 11
I need to identify and tally individual and cross group membership (i.e. how many IDs were only in red, how many were in both red and blue, etc.) My desired output is below. Please note that the IDs column is simply for reference, that column would not be in the expected output.
COLOR TOTAL IDs (reference only, not needed in final output)
RED 2 (7, 13)
BLUE 1 (1)
GREEN 3 (8, 10, 11)
RED, BLUE 3 (2, 3, 6)
RED, GREEN 2 (6, 9)
BLUE, GREEN 2 (4, 6)
RED, BLUE, GREEN 1 (6)
Does anyone know an efficient way to do this in R? Thanks!