I have a dataset which contains the responses of multiple participants to emotional faces. The participants responded with which emotion they think they saw. There were multiple trials per participant. Imagine, there were 4 answer possibilities: Disgust, Sadness, Anger, and Neutral. I want to calculate the proportion each participant selected each answer possibility. When I recently asked a similar question, I got provided with a solution, which later turned out to be incomplete for my needs. Therefore, I opened this new question.
Here is some example data:
Response <- c("Disgust", "Sadness", "Disgust", "Anger", "Anger", "Neutral", "Anger", "Disgust", "Happiness") #create example data
ResponseNum <- c(1,2,1,3,3,4,3,1,5) #Response, but expressed in Numbers
ppnum <- c(1,1,1,2,2,2,3,3,3)
df2a_anger <- as.data.frame(cbind(Response, ResponseNum, ppnum)) #create dataframe
df2a_anger$ResponseNum <- as.numeric(as.character(df2a_anger$ResponseNum)) # make numeric
Here is some example code:
library(dplyr)
df2a_anger %>%
count(ppnum, ResponseNum) %>%
group_by(ppnum) %>%
mutate(n = n/sum(n))
An alternative I found myself trying to solve my problem involves the aggregate function, however both suffer the same problem: They do not count when an response option has not been chosen: For example, the code outputs that participant 1 chose Disgust 66% and Sadness 33%. I also want it to output that participant 1 chose neutral and angry for 0%. Does anyone have any idea how to get R to do that?