This question already has an answer here:
I have grouped and summarized a data frame in R so that I now have a table like:
Group | Value | Count
==========================
A | 1 | 4
A | 2 | 2
A | 10 | 4
B | 3 | 2
B | 4 | 4
B | 2 | 3
C | 5 | 3
C | 2 | 6
I am interested in finding out the relative frequency of the value 2 within each group:
Group | Relative freq of 2
==========================
A | 2/(4+2+4) = 0.2
B | 3/(2+4+3) = 0.33
C | 6/(3+6) = 0.67
Is there a simple, elegant way of calculating this in R, other than writing a bunch of code with loops and conditionals? Possibly using dplyr.
You can use the same logic using for loops
Using
dplyr
, after grouping by 'Group', we subset the 'Count' where 'Value' is 2 (assuming there is only a single 'Value' of 2 per each 'Group') and divide by thesum
of 'Count'The corresponding
data.table
option isThis one with sqldf:
Here is a base R solution: