Relative frequencies / proportions with dplyr

前端 未结 9 2382
灰色年华
灰色年华 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:43

    @Henrik's is better for usability as this will make the column character and no longer numeric but matches what you asked for...

    mtcars %>%
      group_by (am, gear) %>%
      summarise (n=n()) %>%
      mutate(rel.freq = paste0(round(100 * n/sum(n), 0), "%"))
    
    ##   am gear  n rel.freq
    ## 1  0    3 15      79%
    ## 2  0    4  4      21%
    ## 3  1    4  8      62%
    ## 4  1    5  5      38%
    

    EDIT Because Spacedman asked for it :-)

    as.rel_freq <- function(x, rel_freq_col = "rel.freq", ...) {
        class(x) <- c("rel_freq", class(x))
        attributes(x)[["rel_freq_col"]] <- rel_freq_col
        x
    }
    
    print.rel_freq <- function(x, ...) {
        freq_col <- attributes(x)[["rel_freq_col"]]
        x[[freq_col]] <- paste0(round(100 * x[[freq_col]], 0), "%")   
        class(x) <- class(x)[!class(x)%in% "rel_freq"]
        print(x)
    }
    
    mtcars %>%
      group_by (am, gear) %>%
      summarise (n=n()) %>%
      mutate(rel.freq = n/sum(n)) %>%
      as.rel_freq()
    
    ## Source: local data frame [4 x 4]
    ## Groups: am
    ## 
    ##   am gear  n rel.freq
    ## 1  0    3 15      79%
    ## 2  0    4  4      21%
    ## 3  1    4  8      62%
    ## 4  1    5  5      38%
    

提交回复
热议问题