I have a database, in which the a
variable is the group variable and b
is a variable with some categories. My goal is, within each group of a
, to return what else appears in b
.
Consider dput
:
dataset=structure(list(a = c(500, 500, 500, 400, 400, 400, 300, 300,
300), b = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("a",
"b"), class = "factor")), class = "data.frame", row.names = c(NA,
-9L))
Desired result:
a b
500 a
400 b
500 a
In addition, it would be useful to return the counts and percentages of this prevalence. Something like:
a b count percent
500 a 2 .66 #66%
400 b 2 .66 #66%
500 a 2 .66 #66%