dataframe - How to count and calculate percentages for two columns in an R data.frame? -
in r, have data.frame this:
df1 <- data.frame( grade = rep(letters[1:5], 4), sex = c(rep("male", 5), rep("female", 5), rep("male", 4), rep("female", 6)), class = c(rep(1, 10), rep(2, 10)) ) df1 grade sex class 1 male 1 2 b male 1 3 c male 1 4 d male 1 5 e male 1 6 female 1 7 b female 1 8 c female 1 9 d female 1 10 e female 1 11 male 2 12 b male 2 13 c male 2 14 d male 2 15 e female 2 16 female 2 17 b female 2 18 c female 2 19 d female 2 20 e female 2
i want count percentage of sex in each class , make data.frame like:
class male_percent female_percentage 1 50% 50% 2 40% 60%
can teach me how it? question might have been asked before, don't know what's keyword question. sorry if ask same question again.
using data.table
package can following
setdt(df)[ , .( male_percent = paste0(( nrow(.sd[sex == "male"]) / .n ) * 100 , "%") , female_percent = paste0(( nrow(.sd[sex == "female"]) / .n ) * 100 , "%") ) , = class ]
result
# class male_percent female_percent # 1: 1 50% 50% # 2: 2 40% 60%
another dplyr
solution
df %>% group_by(sex , class) %>% summarise(n = n()) %>% group_by(class) %>% summarise( male_percent = paste0((n[sex == "male"] / sum(n)) * 100 , "%") , female_percent = paste0((n[sex == "female"] / sum(n) * 100) , "%") ) # class male_percent female_percent # 1 50% 50% # 2 40% 60%
Comments
Post a Comment