rm(list = ls(all = TRUE))Exercise 3
Begin this practical by setting the maximum line length in R-Studio to 80 characters.
- Go to Preferences (or Global Options under Tools) –> Code –> Display.
- Tick the Show margin box.
- Set the Margin column to 80.
Data I/O
Install the mice package
Use the install.packages() function to install the the mice package.
Load the mice package
Use the library() function to load the mice package.
View the mammalsleep data
Most packages have datasets included. Open the mammalsleep dataset from the mice package in two ways:
- By evaluating its name,
mammalsleep, directly - By using the
View()function
Write the mammalsleep dataset to disk
Save the mammalsleep dataset that you viewed above to your working directory.
- Save the data as a tab-delimited text file.
- Use the
.character as the decimal separator. - Name the file
mammalsleep.txt
Read the mammalsleep.txt file from disk
Read in the data that you just saved to disk and save it as a new R object called sleepdata.
Working with Data
Explore the data
The dataset you’ve just imported contains the sleep data from Allison and Cicchetti (1976).
- Explore these data, and familiarize yourself with the characteristics of the dataset.
Subset the data
Some animals were not used by Allison and Cicchetti (1976).
- Exclude the following animals from
sleepdata:- Echidna
- Lesser short-tailed shrew
- Musk shrew
- Save the dataset as
sleepdata2
Plot brain weight as a function of species
Use the sleepdata2 dataset and base R graphics routines to create a plot of brain weight against species.
Conditional case selection
Some animals have much heavier brains than other animals. Find the names of all animals that have a brain weight larger than 1 standard deviation above the mean brain weight.
Plot of big-brained animals
Replicate the plot from @cref-plot with only the animals you flagged in @cref-flag
- Do not plot any information about the other animals.
Workspace I/O
Save the current workspace
Now that we have imported some data and done some analyses and data manipulations, we may want to save the current workspace (i.e. the current state of our R session). Saving the workspace will save everything in the R session exactly as it exists at the moment of saving. So, we can easily continue from this exact state at a later time. All we need to do is re-load the saved workspace file.
Use the save.image() function to save the entirety of the current workspace.
- Name the workspace image
practical3.RData.
Also, use the save() function to save the sleepdata dataset as a separate workspace.
- Name this workspace
sleepdata.RData.
Clear the workspace
Run the following command to clear the workspace.
This is a very handy line of code to memorize. It will clear nearly everything from your current workspace. If you’re curious about how it does so, check the help files for the rm() and ls() functions.
Load a saved workspace
Use the load() function to load the practical3.RData workspace that you saved in @cref-save.
Load a saved dataset
- Use the
rm()function to remove thesleepdatadataset from the environment. - Use the
load()function to reload thesleepdatadataset from the sleepdata.RData workspace you saved in @cref-save
A better way to read/write R data objects
You may have noticed that when you load a dataset with read.table(), you assign the result to a new R object. However, when you load a dataset saved as a workspace using the load() function, you cannot rename the resulting R object.
When saving an R object with the save() function and loading it with the load() function, the object keeps the name it had when saved. When saving and loading individual data objects, this behavior is rarely desirable.
The saveRDS() and readRDS() functions allow us to save and load R objects in R Data Set (RDS) format.
- Objects stored in RDS format do not keep their original names.
- We have to give the saved object to a new name when we load it with the
readRDS()function. - This workflow is more transparent than the behavior of
save()andload()and better follows the R philosophy of assigning values to objects.
If you need to save individual R objects (i.e., not an entire workspace image), you should probably save them as RDS files and not RData workspaces.
- Use the
saveRDS()function to save thesleepdataobject as sleepdata.rds. - Use the
readRDS()function to load the sleepdata.rds file and assign it to thesleepdata4object.
A Useful Package for Data I/O
If R is not (yet) your preferred data-analysis software, you are probably accustomed to processing your data in some other software and storing data in formats other than RData or RDS. In R, there are many facilities for importing and exporting data with diverse formats.
Here, I want to specifically highlight the haven package written by Hadley Wickham. The haven package provides many useful functions to import and export data from software such as Stata, SAS, and SPSS.
End of Exercise 3
—
Copyright Hanne Oberman, 2025 - CC BY-NC-SA 4.0
Materials developed by Amices team - Methodology & Statistics - Utrecht University