I have the following structured table (as an example):
Class 1 Class 2
1 1 1
2 1 1
3 1 1
4 1 2
5 3 3
6 3 3
7 3 4
8 4 4
I want to count how many times in a given Class 1 the same value appear in Class 2 and display this as a percentage value. Also group class 1. So I would want the result to be something like this:
Class 1 n_class1 Percentage of occurrence in class 2
1 1 4 0.75
2 3 3 0.666
3 4 1 1.0
I have read a lot about the dplyr package and think the solution can be in there, and also looked at many examples but have not yet found a solution. I'm new to programming so don't have the natural programmer thinking yet, hope someone can give me tips on how to to this.
I have manage to get the n_class1
by using group by but struggling to get the the percentage of occurrence in class 2.