-2

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?

phost
  • 19
  • 1
  • 4
  • 1
    Possible duplicate of [dplyr: finding percentage in a sub-group using group\_by and summarise](http://stackoverflow.com/questions/29549731/dplyr-finding-percentage-in-a-sub-group-using-group-by-and-summarise) Or [Summarizing by subgroup percentage in R](http://stackoverflow.com/questions/27134516/summarizing-by-subgroup-percentage-in-r) – Ronak Shah Feb 16 '17 at 17:40

1 Answers1

-1

In R, we can do this by dividing by the sum of 'count' after grouping by 'sleep'

library(dplyr)
df1 %>% 
    group_by(sleep) %>%
    mutate(prop = round(count/sum(count), 3))
#    sleep health count  prop
#    <int>  <chr> <int> <dbl>
#1     7   Good   100 0.500
#2     7 Normal    75 0.375
#3     7    Bad    25 0.125
#4     8   Good   125 0.556
#5     8 Normal    75 0.333
#6     8    Bad    25 0.111

Or using base R

df1$prop <- with(df1, ave(count, sleep, FUN=prop.table))
akrun
  • 874,273
  • 37
  • 540
  • 662