r - Aggregate or summarize to get ratios -


the following toy problem demonstrates question.

i have data frame contains bunch of employees; each employee, has name, salary, gender , state.

aggregate(salary ~ state)  # returns average salary per state aggregate(salary ~ state + gender, data, fun = mean)  # avg salary per state/gender 

what need summary of fraction of total salary earned women in each state.

aggregate(salary ~ state + gender, data, fun = sum)   

returns total salary earned women (and men) in each state ,but need salary_w / salary_total on per-state level. can write for-loop, etc -- wondering if there way use aggregate that.

another option using plyr. ddply() expects data.frame input , return data.frame output. second argument how want split data frame. third argument want apply chunks, here using summarise create new data.frame existing data.frame.

library(plyr)  #using sample data kohske's answer above  > ddply(d, .(state), summarise, ratio = sum(salary[gender == "woman"]) / sum(salary))   state     ratio 1     1 0.5789860 2     2 0.4530224 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -