Relative frequencies / proportions with dplyr

前端 未结 9 2285
灰色年华
灰色年华 2020-11-22 09:25

Suppose I want to calculate the proportion of different values within each group. For example, using the mtcars data, how do I calculate the relative f

9条回答
  •  逝去的感伤
    2020-11-22 09:32

    You can use count() function, which has however a different behaviour depending on the version of dplyr:

    • dplyr 0.7.1: returns an ungrouped table: you need to group again by am

    • dplyr < 0.7.1: returns a grouped table, so no need to group again, although you might want to ungroup() for later manipulations

    dplyr 0.7.1

    mtcars %>%
      count(am, gear) %>%
      group_by(am) %>%
      mutate(freq = n / sum(n))
    

    dplyr < 0.7.1

    mtcars %>%
      count(am, gear) %>%
      mutate(freq = n / sum(n))
    

    This results into a grouped table, if you want to use it for further analysis, it might be useful to remove the grouped attribute with ungroup().

提交回复
热议问题