1

我有一个员工数据的文本文件,其中包括员工收到的加薪、员工是否协商加薪以及员工的性别。我要做的是根据员工的性别以及他们是否协商加薪,创建一个员工收到的加薪金额的直方图。当我运行我的代码时,我不断收到此错误:无效的“中断”数。有人可以告诉我哪里出错了。

这是文本文件的示例:

emp   received   negotiated   gender   year
25    12.5         TRUE         F      2013
318   5.2          FALSE        F      2013
417   9.8          FALSE        M      2009
523   6.8          TRUE         M      2009
1218  2.1          TRUE         F      2009
2601  13.9         FALSE        M      2006
2225  7.8          TRUE         M      2006
3000  8.5          FALSE        F      2006

这是我正在尝试做的一些最小代码:

d<-read.csv("employees.txt", header=TRUE, sep="\t")
cat("\nDisplay what was loaded:")
str(d)


abline(m)
abline(mean(d$received), 0, lty=2)

hist(d$received[d$negotiated == TRUE && d$gender == '1'], main = 'Male Employees Who Negotiated Raises' )

dev.new()
hist(d$received[d$negotiated == TRUE && d$gender == '2'], main = 'Female Employees Who Negotiated Raises' )
4

1 回答 1

2

你的语法有一些问题。

请注意,您已将性别转换为factor值为“1”和“2”而不是“M”和“F”的变量。如果您逐行运行代码,我猜它应该可以工作到您的最后一组直方图。

将这些行更改为:

hist(d$received[d$negotiated == TRUE & d$gender == 2], 
     main = 'Male Employees Who Negotiated Raises' )

hist(d$received[d$negotiated == TRUE & d$gender == 1], 
     main = 'Female Employees Who Negotiated Raises' )

另外,请注意我更改&&&. 运行d$negotiated == TRUE & d$gender == 2d$negotiated == TRUE && d$gender == 2查看它们之间的不同之处。

于 2013-11-10T08:29:56.553 回答