2

我正在使用来自 Sleuth2 库的 ex0622 数据

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

我运行前面的代码向数据集添加一些因素。然后我想用 lattice 包显示某些变量组合

histogram(~Volume[sex.pref=="Hetero"]|gender, data=ex0622_alt, main="Heterosexuals")
dotplot(Volume[sex.pref=="Hetero"]~gender,  col=1)

这两种尝试都会在不应该的情况下产生因素gender和sex.pref的空组合。我不知道发生了什么。

任何帮助,将不胜感激!

谢谢!

4

1 回答 1

3

您的问题出在histogram调用中:在ex0622_alt数据框中,您将Volume变量设置为子集sex.pref == "Hetero",但您根本没有设置gender变量的子集,因此子Volume向量和gender变量的长度不同,因此结果将是奇怪的。如果你这样做,它会起作用:

histogram(~Volume[sex.pref=="Hetero"] | 
           gender[sex.pref=='Hetero'], data=ex0622_alt, main="Heterosexuals")

Or you could just use the subset arg, which would be more natural:

histogram(~Volume | gender, 
          data = ex0622_alt, subset = sex.pref == 'Hetero', main="Heterosexuals")

Same comment (and fix) applies to the dotplot command.

于 2011-02-20T00:26:49.100 回答