2019-07-03

How to calculate the percent of column total in R

Problem

We want to calculate the percent of column total in R. In our example, the percent of column freq: 7/397, 23/397, etc.

    x freq
1 Jan    7
2 Feb   23
3 Mar   86
4 Apr  281
Data:

df <- read.table(text = "x    freq
                        Jan   7
                        Feb   23
                        Mar   86
                        Apr   281", 
                        header = TRUE)

Solution

We create the percent of column total using the function prop.table.

df$prob <- prop.table(df$freq)
# Percentages with two decimal places
df$prob <- round(prop.table(df$freq), 4)*100
    x freq  prob
1 Jan    7  1.76
2 Feb   23  5.79
3 Mar   86 21.66
4 Apr  281 70.78
If we'd like to calculate the percent of a specific row, February in our example:
prop.table(df$freq)[df$x == "Feb"] 
 [1] 0.05793451

Alternatives

  • Base package
  • df$prob <- df$freq/sum(df$freq)
    
  • dplyr
  • library(dplyr)
    df %>% mutate(prob = prop.table(freq))
    # Or
    df %>% mutate(prob = freq / sum(freq))
    
    A specific row:
    df %>% filter(x == "Feb")
    
        x freq       prob
    1 Feb   23 0.05793451
    

References

No hay comentarios:

Publicar un comentario

Nube de datos