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
Asked
Active
Viewed 67 times
0
-
4To 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
-
1Your 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
-
1As 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 Answers
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:
- Because of the precedence of the
&
operator and missing parantheses around the or clause, the logical expression will select males withs22 == 2
as well as all other rows withs22 == 4
regardless of gender. s22_2or4_male
already contains the selected values of columnmydata1$second
. So,hist(mydata1$second[s22_2or4_male])
will use the values ofs22_2or4_male
to subsetmydata1$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])

Uwe
- 41,420
- 11
- 90
- 134