Let say I have data(df) with two columns. Part of it is given below.
familyGroup Sex
1 601 2
2 601 1
3 601 2
4 601 1
5 601 1
6 601 2
7 602 2
8 602 1
9 602 2
10 602 1
11 602 1
12 602 1
Under Sex column <1 represent male> and <2 represent female>. I want the sum of count of male and female for a familyGroup and report in two new columns (say under male and female)
I was using dplyr
from tidyverse
package
Did some trial with group_by
and summary
and mutate
I tried the following code using dplyr
.
However the output is not as expected.
df %>%
group_by(familyGroup, Sex) %>%
summarise(male = sum(Sex==1), female = sum(Sex == 2)) %>%
select(familyGroup, male, female)
# A tibble: 74 x 3
# Groups: familyGroup [40]
familyGroup male female
<fct> <int> <int>
1 601 3 0
2 601 0 3
3 602 4 0
4 602 0 2
The expected output look like the following
familyGroup male female
601 3 3
602 4 2