I want to add columns to a summarized data frame that counts a particular factor.
bookplace <- data.frame(type = c("reading", "reading", "reading", "reading", "lending", "lending"),
sex = c("male", "female", "male", "female", "male", "female"),
usage = c(103, 102, 23, 14, 16, 8),
date = c("1/1/18","1/1/18","1/1/18","1/1/18","1/1/18","1/1/18"),
stringsAsFactors = FALSE)
The result should be (considering male and female as the added columns):
year type users male female
2018 lending 24 16 8
2018 reading 242 126 116
I tried using mutate to add the column and then summarize with the following code:
bookplace %>%
mutate(males=count(sex=="male"),
females=count(sex=="female")) %>%
group_by(year=format(date,"%Y"), type) %>%
summarize(users=sum(usage))
But I have the following error message:
Error in UseMethod("groups") : no applicable method for 'groups' applied to an object of class "logical"
Please, any help will be greatly appreciated.