Appendix
Answers to Exercises
This appendix provides the solutions for the end-of-chapter exercises located in Chapters 112.
Chapter 1
Exercise 1 Solution
To install the coin library/package you need to type the following command:
>install.package(coin)
Note that the name must be in quotes. Then you select the closest mirror site and the package is downloaded and installed for you.
Exercise 2 Solution
To load the coin library and make it ready to use you type the following:
>library(coin)
Once the library is active its commands are available for you to use. You can try to bring up the help entry for the package using the following:
>help(coin)
This does not work for all packages; a better alternative is to use the HTML help system by typing:
>help.start()
This opens the main R help files in your default browser. You can now follow the links to the packages and then find coin from the list.
Exercise 3 Solution
The MASS package is already loaded as part of the R installation but is not ready to use until you type:
>library(MASS)
Once you have the library available you can open the help entry using:
>help(bcv)
Exercise 4 Solution
You can use the search( ) command to get a list of objects that are available for use.
Exercise 5 Solution
To clear the coin package from memory (and remove it from the search( ) path), type the following:
>detach(package:coin)
Chapter 2
Exercise 1 Solution
You can use either the c( ) command or the scan() command to enter these data. The problem is that the bee names contain spaces, which are not allowed. You must alter the names to remove the spaces; the period is the simplest solution.
If you decided to use the c( ) command then the first vector would be created like so:
> Buff.tail = c(10, 1, 37, 5, 12)
If you decided to use the scan( ) command then the process is in two parts. The first part is to initiate the data entry like so:
> Buff.tail = scan()
The second part is to enter the data:
> 10 1 37 5 12
To finish the data entry process you must enter a blank line.
If you decide to use the c( ) command, the entire data entry process would look like the following:
> Buff.tail = c(10, 1, 37, 5, 12)> Garden.bee = c(8, 3, 19, 6, 4)> Red.tail = c(18, 9, 1, 2, 4)> Honey.bee = c(12, 13, 16, 9, 10)> Carder.bee = c(8, 27, 6, 32, 23)
Exercise 2 Solution
You can use the ls( ) command to list all the objects currently in memory. However, there will often be quite a few other objects so you can narrow your display by using a regular expression like so:
> ls(pattern = 'tail|bee')
Note that you must not include a space on either side of the | pipe character. This listing shows all objects that contain tail or bee. You could also list objects that ended with tail or bee by adding the dollar sign as a suffix like so:
> ls(pattern = '.tail$|.bee$')
To save the data objects you can use the save( ) command. The names of the objects could be typed into the command to make a long listing, but this is tedious. The regular expression you typed earlier can be used to produce the list of objects to be saved like so:
> save(list = ls(pattern = 'tail|bee'), file = 'bee data all.RData')
To remove the unwanted individual vectors you need to use the rm( ) command. The names of the objects could be typed into the command as a list or the regular expression could be used once again. This time you must ensure that you do not remove the bees data frame so type the ls( ) command first to check:
> ls(pat = 'tail$|bee$')
Now the $ suffix ensures that you only selected those objects that ended with the text. You can use the up arrow to recall the command and edit it to form the rm( ) command like so:
> rm(list = ls(pat = 'tail$|bee$'))
Note that you must use the list = instruction to ensure that the result of the ls( ) part is treated like a list.
Now quit R using the q( ) command and select No when asked of you want to save the workspace. Restart R and use the ls( ) command once again. All the bee data are gone. To retrieve the data you use the load( ) command.
> load(bee data all.RData)
This command retrieves the data that you saved earlier; the individual vectors that you made are all included in the one file. If you are using Windows or Macintosh you can also use the file.choose( ) instruction rather than the explicit filename like so:
> load(file.choose())
You can also load the data by double clicking on the appropriate file from a Windows Explorer or Mac Finder window.
Chapter 3
Exercise 1 Solution
You can use either the c( ) command or the scan() command to enter these data. The problem is that the bee names contain spaces, which are not allowed. You must alter the names to remove the spaces; the period is the simplest solution.
If you decided to use the c( ) command then the first vector would be created like so:
> Buff.tail = c(10, 1, 37, 5, 12)
If you decided to use the scan( ) command then the process is in two parts. The first part is to initiate the data entry like so:
> Buff.tail = scan()
The second part is to enter the data:
> 10 1 37 5 12
To finish the data entry process you must enter a blank line.
If you decide to use the c( ) command the entire data entry process would look like the following:
> Buff.tail = c(10, 1, 37, 5, 12)> Garden.bee = c(8, 3, 19, 6, 4)> Red.tail = c(18, 9, 1, 2, 4)> Honey.bee = c(12, 13, 16, 9, 10)> Carder.bee = c(8, 27, 6, 32, 23)
To create a data frame you must decide on a name for the object and then use the data.frame( ) command like so:
> bees = data.frame(Buff.tail, Garden.bee, Red.tail, Honey.bee, Carder.bee)
To create row names you can use either the row.names( ) or rownames() command. The plant names also contain spaces, which need to be dealt with as before by replacing with a full stop. The shortest method is to assign the names as a simple list of data like so:
> row.names(bees) = c('Thistle', 'Vipers.bugloss', 'Golden.rain', 'Yellow.alfalfa', 'Blackberry')
You might also decide to create a vector to hold the plant names as a separate object:
> plant.names = c('Thistle', 'Vipers.bugloss', 'Golden.rain', 'Yellow.alfalfa', 'Blackberry')> row.names(bees) = plant.names
This last method is slightly longer but the vector of plant names can be useful for other purposes.
Exercise 2 Solution
To make a matrix you need the data as separate columns (or rows) or as a single vector of values. You already have the separate vectors for the different bees so begin by using the cbind( ) command to join them column by column into a new matrix:
> beematrix = cbind(Buff.tail, Garden.bee, Red.tail, Honey.bee, Carder.bee)
Your new matrix will not contain any row names so to include them you need to use the rownames( ) command:
> plant.names = c('Thistle', 'Vipers.bugloss', 'Golden.rain', 'Yellow.alfalfa', 'Blackberry')> rownames(beematrix) = plant.names
The second way to create a matrix is to use a single vector of values and use the matrix( ) command. To create a single vector you could combine the individual bee vectors:
> bee.data = c(Buff.tail, Garden.bee, Red.tail, Honey.bee, Carder.bee)