I am using the ex0622 data from library Sleuth2
library(Sleuth2)
library(lattice)
attach(ex0622)
#Using the 'rep()' function to create a vector for the sexual preference variable ('Hetero' or 'Homo')
sex.pref=as.factor(c(rep("Hetero", 16), rep("Homo", 19), rep("Hetero", 6)))
#Using the 'rep()' function to create a vector for the Type of Death variable ('AIDS' or 'Non-AIDS')
death.type=c(rep("Aids",6), rep("Non-Aids",10), rep("Aids", 19), "Aids", rep("Non-Aids", 5))
#creating a vector of gender variable
gender=(c(rep("Male", 35), rep("Female", 6)))
length(death.type)
ex0622_alt=as.data.frame(cbind(ex0622, gender, sex.pref, death.type))
ex0622_alt
I run the preceeding code to add some factors to the data set. Then I want to display certain combinations of variables with the lattice package
histogram(~Volume[sex.pref=="Hetero"]|gender, data=ex0622_alt, main="Heterosexuals")
dotplot(Volume[sex.pref=="Hetero"]~gender, col=1)
Both those attempts produce empty combinations of the factors gender and sex.pref when they should not. I have no idea what is going on.
Any help would be appreciated!
Thanks!
Your problem is in the
histogramcall: Within theex0622_altdata-frame, you’re subsetting theVolumevariable bysex.pref == "Hetero", but you’re not subsetting thegendervariable at all, so theVolumesubvector and thegendervariable don’t have the same length, so results will be strange. It works if you do:Or you could just use the
subsetarg, which would be more natural:Same comment (and fix) applies to the
dotplotcommand.