Here is an example of my data frame:
Question Student Option
1 1 1 4
2 1 2 1
3 1 3 1
4 1 4 5
5 2 1 3
6 2 2 2
7 2 3 2
8 2 4 1
I am trying to compute the percentage of response by option 0-5 for each question. I manage to compute the percentage of response for each option by question like this:
Global_Response <- group_by(ExamData,Question) %>% #group data by question
summarise(Global_0 = length(which(Option=="0"))/length(Option)*100, #Percentage of response option "0" by question
Global_1 = length(which(Option=="1"))/length(Option)*100,
Global_2 = length(which(Option=="2"))/length(Option)*100,
Global_3 = length(which(Option=="3"))/length(Option)*100,
Global_4 = length(which(Option=="4"))/length(Option)*100,
Global_5 = length(which(Option=="5"))/length(Option)*100)
In the end, I would like to have a new data frame with the percentage of response for each option. Similar to this example:
Question Global_0 Global_1 Global_2 Global_3 Global_4 Global_5
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0 50.0 0 0 25.0 25.0
2 0 25.0 50.0 25.0 0 0
Now, I would like to do the same thing but with a few lines of code. I have tried a for loop, but it didn't work. I don't know what would be more appropriate to do what I want.