2017-09-13 90 views
1

在R中,使用dplyr,我有一個數據表,它具有獨立變量性別和政黨,以及因變量薪金。我可以找到男性/女性和勞工與保守黨的平均薪水,但我想要保守黨男子,保守黨婦女,勞工男子和勞工婦女的平均薪水。我怎麼弄到的?R程序和平均打印結果

我: 工黨保守黨

Male Female Male Female 

£30,000 £27,000 £25,000 £23,000 
£26,000 £19,000 £24,500 £27,000 
£28,000 £23,000 £26,000 £21,000 
£35,000 £22,000 £31,000 £22,000 
£35,000 £21,000 £27,000 £30,000 
£37,000 £20,000 £30,000 £30,000 

我需要這樣的東西:

gender party mean(salary) 
1 female labour 27833.33 
2 female tory 23000.00 
3 male labour 27250.00 
4 male tory 31166.67 

回答

1

你需要有完整的數據集,以建立一個功能

但使用aggregate(data = df, salary~gender+party, FUN = mean)