i try to save grouped counts of various factor levels into a new variable:
Lets say my data look like this:
a <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
b <- c("acc", "rej", "con", "acc", "rej", "con", "acc", "rej", "con", "acc", "rej", "con", "acc", "rej", "con","acc", "rej", "con", "acc", "rej")
df <- data.frame(a,b)
The resulting data frame should look like this:
a <- c(1,2,3,4)
number_acc <- c(2,2,1,2)
number_rej <- c(2,1,2,2)
number_con <- c(1,2,2,1)
I tried to solve the problem in the following way:
df2 <- df %>%
group_by(a) %>%
mutate(number_acc = count(b == 'acc'),
number_rej = count(b == 'rej'),
number_con = count(b == 'con'))
However, i get an error message that the method "count" cannot be applied to objects of the class "logical".
Thank you for your help!