I have a following dataframe:
sleep health count prop
1 7 Good 100 NA
2 7 Normal 75 NA
3 7 Bad 25 NA
4 8 Good 125 NA
5 8 Normal 75 NA
6 8 Bad 25 NA
I want to fill the prop
column with each percentage of count
based on sleep
group. For instance, the first 3 rows prop
should be 0.5, 0.375, and 0.125 then the last 3 rows prop
are 0.555, 0.333, and 0.111 respectively.
This can be done manually by separating the data frame by sleep
first then use prop.table(prop)
for each, but since there are numerous sleep
group I can't find a succinct way to do this. Any thoughts?