Calculate relative frequency for a certain group

前端 未结 1 597
说谎
说谎 2021-01-22 16:45

I have a data.frame of categorical variables that I have divided into groups and I got the counts for each group.

My original data nyD looks like:

Source: local         


        
相关标签:
1条回答
  • 2021-01-22 17:07

    You can do this whole thing in one step (from your original data nyD and without creating ny1). That is because when you'll run mutate after summarise, dplyr will drop one aggregation level (v2) by default (certainly my favorite feature in dplyr) and will aggregate only by v1

    nyD %>% 
       group_by(v1, v2) %>%
       summarise(count = n()) %>%
       mutate(prop = count/sum(count))
    
    # Source: local data frame [5 x 4]
    # Groups: v1
    # 
    #   v1    v2 count      prop
    # 1  a minus     1 0.3333333
    # 2  a  plus     2 0.6666667
    # 3  b minus     1 0.5000000
    # 4  b     x     1 0.5000000
    # 5  c     x     2 1.0000000
    

    Or a shorter version using count (Thanks to @beginneR)

    df %>% 
      count(v1, v2) %>% 
      mutate(prop = n/sum(n))
    
    0 讨论(0)
提交回复
热议问题