## ----style, echo = FALSE, results = 'asis'--------------------------------- knitr::opts_chunk$set( eval=as.logical(Sys.getenv("KNITR_EVAL", "TRUE")), cache=as.logical(Sys.getenv("KNITR_CACHE", "TRUE"))) ## ----brfss-aggregate-formula----------------------------------------------- aggregate(Weight ~ Year + Sex, brfss, mean) # same, but more informative aggregate(. ~ Year + Sex, brfss, mean) # all variables ## ----t-test-1990----------------------------------------------------------- brfss_1990 = brfss[brfss$Year == 1990,] t.test(Weight ~ Sex, brfss_1990) t.test(Weight ~ Sex, brfss, subset = Year == 1990) ## ----brfss-boxplot, fig.width=5, fig.height=5------------------------------ boxplot(Weight ~ Year, brfss, subset = Sex == "Male", main="Males") ## ----ALL-subset-NA--------------------------------------------------------- idx <- pdata$sex == "F" & pdata$age > 40 table(idx, useNA="ifany") dim(pdata[idx,]) # WARNING: 'NA' rows introduced tail(pdata[idx,]) dim(subset(pdata, idx)) # BETTER: no NA rows dim(subset(pdata, (sex == "F") & (age > 40))) # alternative tail(subset(pdata,idx)) ## robust `[`: exclude NA values dim(pdata[idx & !is.na(idx),]) ## ----ALL-BCR/ABL-subset---------------------------------------------------- bcrabl <- subset(pdata, mol.biol %in% c("BCR/ABL", "NEG"))