1

I have a text file of employee data that includes what raise the employee received, if the employee negotiated for the raise, and the gender of the employee. What I'm trying to do is create a histogram of the amount of a raise an employee received based on the gender of the employee and if they negotiated for the raise or not. When I run my code I keep getting this error: invalid number of 'breaks'. Could someone please show me where I went wrong.

Here's an example of the text file:

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

Here's some minimal code for what I'm trying to do:

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' )
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ML45
  • 47
  • 1
  • 1
  • 7
  • 1
    I believe we may need a reproducible example. We don't need your data, just make some up. Here are some tips on how to do that: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Nov 10 '13 at 08:23
  • @RomanLuštrik Ok I just did that! – ML45 Nov 10 '13 at 08:32
  • possible duplicate of [How to create a histogram based on true or false in R?](http://stackoverflow.com/questions/19886572/how-to-create-a-histogram-based-on-true-or-false-in-r) – Frank Nov 10 '13 at 09:01

1 Answers1

2

You have some problems in your syntax.

Note that you have converted gender to a factor variable with values of "1" and "2" instead of "M" and "F". If you run your code line-by-line, I'll guess that it should work up to your last set of histograms.

Change those lines to:

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' )

Also, notice that I changed && to &. Run d$negotiated == TRUE & d$gender == 2 and d$negotiated == TRUE && d$gender == 2 to see how they are different from each other.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485