# ProjectMOSAIC/Workshops

Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
153 lines (109 sloc) 3.55 KB
 %% Author: Daniel Kaplan %% Subject: R basics %% Title: Study Guide to R Basics % !Rnw weave = knitr \documentclass[10pt]{article} \usepackage{fancyhdr} \usepackage{graphicx, verbatim} \setlength{\textwidth}{6.5in} \setlength{\textheight}{9in} \setlength{\oddsidemargin}{0in} \setlength{\evensidemargin}{0in} \setlength{\topmargin}{0cm} \pagestyle{fancy} \lhead{\textsc{Prof. Horton}} \chead{\textsc{STAT 135: \texttt{R} Study Guide}} \rhead{\textsc{Fall 2014}} \lfoot{} \cfoot{} \cfoot{\thepage} \rfoot{} \renewcommand{\headrulewidth}{0.2pt} \renewcommand{\footrulewidth}{0.0pt} \begin{document} %\SweaveOpts{concordance=TRUE} %\newcommand{\TextEntry}{\vspace*{0in}} <>= require(mosaicData,quietly=TRUE) require(mosaic,quietly=TRUE) require(knitr) opts_chunk$set(fig.width=5, fig.height=2.5) trellis.par.set(theme=col.mosaic()) # get a better color scheme for lattice @ \begin{enumerate} \item Given numerical objects named \texttt{x} and \texttt{y}, calculate this quantity:$ \sqrt{x^2 + y}$<>= sqrt(x^2 + y) @ \item Load the \texttt{mosaic} and \texttt{mosaicData} packages. (We will be using the \texttt{CPS85} data set from \texttt{mosaicData} for our subsequent examples.) <<>>= require(mosaic) require(mosaicData) @ % \item Load the data set named CPS85" (available through the \texttt{mosaicData} %package), and assign it to an object called \texttt{CPS85}. %<>= %# Remember the quotes around the name of the data set %CPS85 = fetchData("CPS85") %@ \item Display the first few rows of the \texttt{CPS85} data frame. <>= head(CPS85) @ \item Display the names of the variables from the data frame. <>= names(CPS85) @ \item Calculate (not count by hand!) the number of cases in the data frame. <>= nrow(CPS85) @ \item Calculate the mean wage of all the people. <>= mean(~ wage, data=CPS85) @ \item Calculate the standard deviation of wage for all cases. <>= sd(~ wage, data=CPS85) @ \item Calculate the mean wage separately for married and unmarried people. <>= mean(wage ~ married, data=CPS85) @ \item Create a new variable, \texttt{fraction}, in the data frame that holds the ratio of the person's experience" to their age. <>= CPS85 <- mutate(CPS85, fraction=exper/age) CPS85 <- CPS85 %>% mutate(fraction = exper/age) @ \item Make a box-and-whisker plot of all the people's CPS85. <>= bwplot(~wage, data=CPS85) @ \newpage \item Make a box-and-whisker plot of the people's wage, but broken down by marital status. <>= bwplot(wage ~ married, data=CPS85) @ \item Make this plot: <>= densityplot(~ age, groups=married, auto.key=TRUE, data=CPS85) @ <>= densityplot(~ age, groups=married, auto.key=TRUE, data=CPS85) @ What is different when the command {\tt densityplot($\sim\$ age | married, data=CPS85)} is run? %<<>>= %densityplot(~ age | married, data=CPS85) %@ \item Calculate (not count by hand!) the number of people by marital status. <>= tally(~ married, data=CPS85) @ \item Calculate (not count by hand!) the number of people by marital status and sex simultaneously. <>= tally(~ married + sex, data=CPS85) @ \end{enumerate} \end{document}