在R中生成代码

时间:2017-11-21 13:42:14

标签: r

请帮助生成以下R代码。我是R的新手,我真的很想知道更多。我只需要一些帮助。

*男性年收入低于女性

Dataframe = DF(具有以下值)

年龄性别年度收入MaritalStatus

54女109000单

22男62541离婚

38男93214已婚

42女62471已婚

35女98741单

42男98741已婚

谢谢。

1 个答案:

答案 0 :(得分:0)

这绝对是重复的,但是你走了:

df <- read.table(text = "Age Gender AnnualIncome MaritalStatus

54 Female 109000 Single

22 Male 62541 Divorced

38 Male 93214 Married

42 Female 62471 Married

35 Female 98741 Single

42 Male 98741 Married", header = TRUE)

结果:

> df
  Age Gender AnnualIncome MaritalStatus
1  54 Female       109000        Single
2  22   Male        62541      Divorced
3  38   Male        93214       Married
4  42 Female        62471       Married
5  35 Female        98741        Single
6  42   Male        98741       Married

比较每组(df$Gender)的含义:

aggregate(df$AnnualIncome, list(df$Gender), mean)

> aggregate(df$AnnualIncome, list(df$Gender), mean)
  Group.1        x
1  Female 90070.67
2    Male 84832.00