0

I am trying to create a histogram that will summarize the distribution of second finger length for males who answered question s22 with either 2 or 4. I am experiencing errors where it says invalid breaks, how do I fix this

  • 4
    To give a [good reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), it is helpful to provide some of your data via `dput(head(df))`, then edit and paste the result into your question. – AndrewGB Jul 02 '21 at 22:46
  • 1
    Your filter is incorrect, try this instead `mydata1$gender == 'male' & ( mydata1$s22 =='2' | mydata1$s22 == '4')`. You want an OR and not an AND between the =2 & =4 part. – Dave2e Jul 02 '21 at 23:29
  • 1
    As already mentioned it would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 03 '21 at 03:33

1 Answers1

1

OP's last edit of the question (as of 2021-07-03 03:55Z) shows the code

s22_2or4_male <- mydata1$second[mydata1$gender == 'male' & mydata1$s22 =='2' | mydata1$s22 == '4']
  
hist(mydata1$second[s22_2or4_male])

which has several issues:

  1. Because of the precedence of the & operator and missing parantheses around the or clause, the logical expression will select males with s22 == 2 as well as all other rows with s22 == 4 regardless of gender.
  2. s22_2or4_male already contains the selected values of column mydata1$second. So, hist(mydata1$second[s22_2or4_male]) will use the values of s22_2or4_male to subset mydata1$second again.

The issues can be fixed by

s22_2or4_male <- mydata1$gender == 'male' & mydata1$s22 %in% c('2', '4')
hist(mydata1$second[s22_2or4_male])

Now, s22_2or4_male is a logical vector indicating the selected rows of mydata1.

Reproducible example using the mtcars dataset

cyl_6or8_gear4 <- mtcars$gear == 4 & mtcars$cyl %in% c(6, 8)
hist(mtcars$mpg[cyl_6or8_gear4])

enter image description here

Uwe
  • 41,420
  • 11
  • 90
  • 134