R程序和平均结果打印

时间:2017-09-13 16:35:06

标签: r dplyr

在R中,使用dplyr,我有一个数据表,它具有自变量性和政党,以及因变量薪水。我可以找到男性/女性和工党与保守党的平均工资,但我想要保守党男性,保守党女性,劳工男性和工党女性的平均工资。我怎么能得到它?

我有: 劳工保守党

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 个答案:

答案 0 :(得分:1)

您需要拥有完整的数据集来构建函数

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