# YouthRisk 2007 data from Division of Adolescent and School Health, National Center for HIV/AIDS, Viral Hepatitis, STD, and TB Prevention install.packages("Stat2Data") # only if you have not done so already or the the package has not been installed as part of R installation libarary(Stat2Data) attach(YouthRisk) head(YouthRisk) number16and17yearolds<-length(which(age4>16)) ages<-levels(as.factor(age4)) tapply(age4,smoke,sum,na.rm=TRUE) # 0 1 # 96400 112756 tapply(female,smoke,sum,na.rm=TRUE) # 0 1 #1857 4642 YouthsByAge<-apply(agetable,1,sum) # 14 15 16 17 18 #1183 2883 3287 3262 1970 agetable<-table(age4,smoke) agetable # smoke #age4 0 1 # 14 739 468 # 15 1595 1397 # 16 1579 1798 # 17 1349 2001 # 18 774 1248 sextable<-table(age4,female) sextable #age4 0 1 # 14 903 280 # 15 1709 1174 # 16 1587 1700 # 17 1190 2072 # 18 558 1412 smoketable<-table(female,smoke) smoketable # smoke #female 0 1 # 0 3871 1979 # 1 1857 4642 femalesOnly<-subset(YouthRisk,female==1) femaleSmokersByAge<-table(femalesOnly$age4,femalesOnly$smoke) femaleSmokersByAge # 0 1 # 14 84 188 # 15 315 823 # 16 493 1173 # 17 557 1468 # 18 401 970 ProportionFemaleSmokeryByAge<-femaleSmokersByAge[,2]/sextable[,2] ProportionFemaleSmokeryByAge # 14 15 16 17 18 #0.6714286 0.7010221 0.6900000 0.7084942 0.6869688