Search code examples
rhistogram

Making a histogram that selects specific individuals


Sorry, but I am very new using R and have come up with a bit of an issue. I'm trying to make a histogram for all that have gender as male, and their answer to column s22 is either 2 or 4. The column s22 can be any number between 1 and 5.

my code is currently hist(firstdata$second)[(firstdata$gender == "male")&(firstdata$s22 == 2)|(firstdata$s22 == 4)]

The issue with this is that the histogram shows a frequency of individuals higher than the total amount of males (there are 40 males total, 92 individuals, and the histogram appears to show all individuals)

Thanks for the help!


Solution

  • (I would comment instead but I don't have the reputation yet!)

    I would approach this by first creating an object of that subset of data and then plotting it.

    E.g. using the which() function

    male_2_4 <- firstdata[which(firstdata$gender == "male" & (firstdata$s22 == 2 | firstdata$s22 == 4),]
    hist(male_2_4$second)
    

    Remember to specify if you want the or (|) to be just between either s22 = 2 or s22 = 4, then there needs to be brackets around those two arguments, otherwise it will not be separate from the firstdata$gender == "male" argument.

    If you're new to using which() make sure you remember to add the last comma inside the square bracket, as you're subsetting by row rather than column.