--- title: 'R for Researchers: Data preparation solutions' date: "April 2015" --- This article contains solutions to exercises for an article in the series R for Researchers. For a list of topics covered by this series, see the [Introduction](RFR_Introduction.html) article. If you\'re new to R we highly recommend reading the articles in order. There is often more than one approach to the exercises. Do not be concerned if your approach is different than the solution provided. #### Exercise solutions Do the following exercise in the AlfAnalysis script. ```{r, echo=FALSE, results="hide", message=FALSE, warning=FALSE} ####################################################### ####################################################### ## ## Environment Setup ## ####################################################### ####################################################### library(faraway) # glm support library(MASS) # negative binomial support library(car) # regression functions library(lme4) # random effects library(ggplot2) # plotting commands library(reshape2) # wide to tall reshaping library(xtable) # nice table formatting library(knitr) # kable table formatting library(grid) # units function for ggplot saveDir <- getwd() # get the current working directory saveDir # show me the saved directory wd <- "u:/RFR" # path to my project # setwd(wd) # set this path as my work directory ``` 1. Import the dataset in alfalfa.txt from the course Dataset folder. ```{r, comment=NA } ####################################################### ####################################################### ## ## Import Data ## ####################################################### ####################################################### alfalfaIn <- read.table("Datasets/alfalfa.txt", header=TRUE) str(alfalfaIn) alfalfa <- alfalfaIn ``` 3. Change the variable names to \"shade\",\"irrig\", \"inoc\", and \"yield\" ```{r, comment=NA } colnames(alfalfa) <- c("shade","irrig","inoc","yield") ``` 4. Create a new variable for shade level (shadeLev) from the shade variable by setting 1 to \"full\", 5 to \"none\", and the rest to \"part\". ```{r, comment=NA } shadeLev <- cut( alfalfa$shade, c(0,1.5,4.5,6), labels=c("full","part","none") ) str(shadeLev) ``` 5. Change the type of shadeLev to factor. ```{r, comment=NA } shadeLev <- factor(shadeLev) ``` 6. Include shadeLev in the alfalfa data.frame. ```{r, comment=NA } alfalfa <- data.frame(alfalfa, shadeLev=shadeLev ) ``` 7. Change inoculate level E to control. This is a more challenging problem. ```{r, comment=NA } alfalfa$inoc <- ifelse(alfalfa$inoc=="E","cntrl", as.character(alfalfa$inoc) ) alfalfa$inoc <- factor(alfalfa$inoc) str(alfalfa) ``` 8. Commit your changes to AlfAnalysis. There is no code associated with the solution to this problem. Return to the [Data presentation](RFR_DataPres.html) article. Last Revised: 2/9/2015