Search code examples
rdplyr

How to calculate % to total for multiple variables?


students <- data.frame( names = c( "Bill", "Stacey", "Fred", "Jane", "Sarah" ), 
                        gender = c( "M", "F", "M", "F", "F" ),
                        age = c( "10-12", "10-12", "13-15", "7-9", "16-18" ),
                        stringsAsFactors = FALSE )

I have used the function below for one variable however, it doesnt work for multiple variables.

library(dplyr)
result_dplyr <- students %>%
group_by(gender) %>%
summarise(per_to_tot = n() / nrow(student) * 100)
print(result_dplyr)

Desired output is below.

#    gender per to tot
# 1:      M 40
# 2:      F 60
#    age  
# 1:     7-9   20
# 2:     10-12 40
# 3:     13-15 20
# 4:     16-18 20

R code that works for multiple variables.


Solution

  • You can try

    lapply(students[-1], \(x) as.data.frame(proportions(table(x)) * 100))
    

    which gives

    $gender
      x Freq
    1 F   60
    2 M   40
    
    $age
          x Freq
    1 10-12   40
    2 13-15   20
    3 16-18   20
    4   7-9   20