### R code from vignette source 'helpSASR2.SASnw' ################################################### ### code chunk number 1: helpSASR2.SASnw:8-9 ################################################### options(continue=" ") ################################################### ### code chunk number 2: helpSASR2.SASnw:59-65 ################################################### options(digits=3) options(width=72) # narrow output ds = read.csv("http://www.amherst.edu/~nhorton/sasr2/datasets/help.csv") newds = subset(ds, select=c("cesd","female","i1","i2","id","treat","f1a", "f1b","f1c","f1d","f1e","f1f","f1g","f1h","f1i","f1j","f1k","f1l", "f1m","f1n","f1o","f1p","f1q","f1r","f1s","f1t")) ################################################### ### code chunk number 3: helpSASR2.SASnw:107-109 ################################################### names(newds) str(newds[,1:10]) # structure of the first 10 variables ################################################### ### code chunk number 4: helpSASR2.SASnw:114-115 ################################################### summary(newds[,1:10]) # summary of the first 10 variables ################################################### ### code chunk number 5: helpSASR2.SASnw:133-134 ################################################### head(newds, n=3) ################################################### ### code chunk number 6: helpSASR2.SASnw:166-169 ################################################### comment(newds) = "HELP baseline dataset" comment(newds) save(ds, file="savedfile") ################################################### ### code chunk number 7: helpSASR2.SASnw:190-191 ################################################### write.csv(ds, file="ds.csv") ################################################### ### code chunk number 8: helpSASR2.SASnw:203-205 ################################################### library(foreign) write.foreign(newds, "file.dat", "file.sas", package="SAS") ################################################### ### code chunk number 9: helpSASR2.SASnw:239-241 ################################################### with(newds, cesd[1:10]) with(newds, head(cesd, 10)) ################################################### ### code chunk number 10: helpSASR2.SASnw:261-262 ################################################### with(newds, which(cesd > 56)) ################################################### ### code chunk number 11: helpSASR2.SASnw:267-268 ################################################### subset(newds, cesd > 56, select=c("cesd")) ################################################### ### code chunk number 12: helpSASR2.SASnw:297-299 ################################################### with(newds, sort(cesd)[1:4]) with(newds, which.min(cesd)) ################################################### ### code chunk number 13: helpSASR2.SASnw:340-343 ################################################### library(mosaic) tally(~ is.na(f1g), data=newds) favstats(~ f1g, data=newds) ################################################### ### code chunk number 14: helpSASR2.SASnw:392-401 ################################################### # reverse code f1d, f1h, f1l and f1p cesditems = with(newds, cbind(f1a, f1b, f1c, (3 - f1d), f1e, f1f, f1g, (3 - f1h), f1i, f1j, f1k, (3 - f1l), f1m, f1n, f1o, (3 - f1p), f1q, f1r, f1s, f1t)) nmisscesd = apply(is.na(cesditems), 1, sum) ncesditems = cesditems ncesditems[is.na(cesditems)] = 0 newcesd = apply(ncesditems, 1, sum) imputemeancesd = 20/(20-nmisscesd)*newcesd ################################################### ### code chunk number 15: helpSASR2.SASnw:422-423 ################################################### cbind(newcesd, newds$cesd, nmisscesd, imputemeancesd)[nmisscesd>0,] ################################################### ### code chunk number 16: createdrink ################################################### library(memisc) newds = transform(newds, drinkstat= cases( "abstinent" = i1==0, "moderate" = (i1>0 & i1<=1 & i2<=3 & female==1) | (i1>0 & i1<=2 & i2<=4 & female==0), "highrisk" = ((i1>1 | i2>3) & female==1) | ((i1>2 | i2>4) & female==0))) library(mosaic) ################################################### ### code chunk number 17: helpSASR2.SASnw:517-519 ################################################### tmpds = subset(newds, select=c("i1", "i2", "female", "drinkstat")) tmpds[365:370,] ################################################### ### code chunk number 18: helpSASR2.SASnw:538-539 ################################################### subset(tmpds, drinkstat=="moderate" & female==1) ################################################### ### code chunk number 19: helpSASR2.SASnw:566-571 ################################################### with(tmpds, sum(is.na(drinkstat))) mytab = rbind(tally(~ drinkstat, data=tmpds), tally(~ drinkstat, format="percent", data=tmpds)) row.names(mytab) = c("count", "percent") mytab ################################################### ### code chunk number 20: helpSASR2.SASnw:585-587 ################################################### tally(~ drinkstat + female, data=tmpds) tally(~ female | drinkstat, data=tmpds) ################################################### ### code chunk number 21: helpSASR2.SASnw:627-630 ################################################### newds = transform(newds, gender=factor(female, c(0,1), c("Male","Female"))) tally(~ female + gender, margin=FALSE, data=newds) ################################################### ### code chunk number 22: helpSASR2.SASnw:663-665 ################################################### newds = ds[order(ds$cesd, ds$i1),] newds[1:5,c("cesd", "i1", "id")] ################################################### ### code chunk number 23: helpSASR2.SASnw:701-705 ################################################### females = subset(ds, female==1) with(females, mean(cesd)) # an alternative approach mean(ds$cesd[ds$female==1]) ################################################### ### code chunk number 24: helpSASR2.SASnw:733-736 ################################################### with(ds, tapply(cesd, female, mean)) library(mosaic) mean(cesd ~ female, data=ds)