Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate % to total for multiple variables?

Tags:

r

dplyr

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.

like image 719
bbear Avatar asked Oct 22 '25 05:10

bbear


1 Answers

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
 
like image 187
ThomasIsCoding Avatar answered Oct 23 '25 19:10

ThomasIsCoding



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!