--- # Exercises Begin this practical exercise by setting the maximum line length in `R-Studio` to 80 characters. Go to `RStudio`'s `Preferences` (or `Global Options` under `Tools`) --> `Code` --> `Display`, and tick the `show margin` box. Make sure that the `margin column` is set to `80` --- ## Exercise 1-5 --- 1. **Install package `mice`. ** Go to `Tools` > `Install Packages` in `RStudio`. If you are connected to the internet, select `Repository` under `Install From` and type `mice` under `Packages`. Leave the `Install to Library` at default and make sure that `Install Dependencies` is selected. Click install. If you are not connected to the internet, select `Package Archive File` under "Install from" and navigate to the respective file on your drive. Some packages depend on other packages, meaning that their functionality may be limited if their dependencies are not installed. Installing dependencies is therefor recommended, but internet connectivity is required. If all is right, you will receive a message in the console that the package has been installed (as well as its dependencies). ALternatively, if you know the name of the package you would like to install - in this case `mice` - you can also call `install.packages("mice")` in the console window. --- 2. **Load package `mice`. ** Loading packages can be done through functions `library()` and `require()`. ```{r} # type your code here ``` If you use `require()` within a function, and the required package is not available, `require()` will yield a warning and the remainder of the function is still executed, whereas `library()` will yield an error and terminate all executions. The use of `library()` when not doing too complicated things is preferred - `require()` would result in more computational overhead because it calls `library()` itself. --- 3. **Most packages have datasets included. Open the `mammalsleep` dataset from package `mice` by typing `mammalsleep` in the console, and subsequently by using the function `View()`. ** Using `View()` is preferred for inspecting datasets that are large. `View()` opens the dataset in a spreadsheet-like window (conform MS Excel, or SPSS). If you `View()` your own datasets, you can even edit the datasets' contents. --- 4. **Write the mammalsleep dataset from package `mice` to the work directory (= the directory of your R project) as a tab-delimited text file with `.` as a decimal separator. Use the function `write.table()` and name the file `mammalsleep.txt`.** ```{r} # type your code here ``` --- 5. **Import the `mammalsleep.txt` file with `read.table()`. ** ```{r} # type your code here ``` --- ## Exercise 6-10 --- 6. **The dataset we've just imported contains the sleepdata by Allison & Cicchetti (1976). Inspect the sleepdata and make yourself familiar with it. ** If you would like to know more about this dataset, you can open the help for the `mammalsleep` dataset in package `mice` through `?mammalsleep`. Inspect the data set to obtain information about the variables, the number of observations. Some summary statistics. ```{r} # type your code here ``` --- 7. **Save the current workspace. Name the workspace `Practical_C.RData`. Also, save the sleepdata file as a separate workspace called `Sleepdata.RData`. ** Now that we have imported our data, it may be wise to save the current workspace, i.e. the current state of affairs. Saving the workspace will leave everything as is, so that we can continue from this exact state at a later time, by simply opening the workspace file. To save everything in the current workspace, type: ```{r } # To save the entire workspace: save.image("Practical_C.RData") ``` To save just the data set `sleepdata`, and nothing else, type: ```{r , eval=FALSE} # To save the data set only. save(sleepdata, file = "Sleepdata.RData") ``` With the save functions, any object in the workspace can be saved. --- 8. **Some animals were not used in the calculations by Allison and Cicchetti. Exclude the following animals from the sleepdata data set: Echidna, Lesser short-tailed shrew and Musk shrew. Save the data set as `sleepdata2`.** Tip: use the square brackets to indicate [rows, columns] or use the function `filter()` from `dplyr`. ```{r} # type your code here ``` --- 9. **Plot brain weight as a function of species. ** ```{r} # type your code here ``` --- 10. **Some animals have much heavier brains than other animals. Find out the names of the animals that have a brain weight larger than 1 standard deviation above the mean brain weight. Replicate the plot from Question 9 with only these animals and do not plot any information about the other animals. ** To find out which animals have a brain weight larger than 1 standard deviation above the mean brain weight: ```{r} # type your code here ``` To plot these animals: ```{r} # type your code here ``` The downside is that it still prints all the animals on the x-axis. This is due to the factor labels for `species` being copied to the smaller subset of the data. Plot automatically takes over the labels. For example, ```{r eval=FALSE} sleepdata2$species[which] ``` returns only 3 mammals, but still has 62 factor levels. To get rid of the unused factor levels, we can use function `factor()`: ```{r eval=FALSE} sleepdata3 <- sleepdata2[which, ] sleepdata3$species <- factor(sleepdata3$species) sleepdata3$species ``` To plot the graph that we wanted: ```{r} # type your code here ``` --- End of exercise ---