Below is a sample DF that illustrates the issue that I am having. I am having an issue with a group not having a value for all variables so R is not returning anything for it. That is, in the data below R returns:
Course Gender n
English1 Female 1
English1 Male 3
English2 Female 2
English2 Male 1
English2 Unknown 1
English3 Female 3
English3 Unknown 1
df1 <- data.frame("Course"=c("English1", "English1", "English1", "English1",
"English2", "English2", "English2", "English2",
"English3", "English3", "English3", "English3"),
Gender=c("Male", "Female", "Male", "Male", "Male", "Female",
"Unknown", "Female", "Female", "Female", "Female",
"Unknown"), Grade=c("A", "A", "C", "D", "D", "A", "B",
"C", "B", "D", "A", "C"))
library(dplyr)
df1 %>% group_by(Course, Gender) %>% count
What I am trying to do is return a Null or 0 when there are not counts of the Gender within the Course group. I would like the data to return this (I tagged the new rows with *):
Course Gender n
English1 Female 1
English1 Male 3
English1 Unknown 0*
English2 Female 2
English2 Male 1
English2 Unknown 1
English3 Female 3
English3 Male 0*
English3 Unknown 1
The reason that I need this is because I need to have identical groups (three genders for each course) for an rMarkdown output. Any help is greatly appreciated