Exercises for 1.3: Some important practical matters

Exercise 1.3.1: Loading a package

  • Use the check boxes in the Rstudio packages tab to load the nlme package
  • Uncheck the box to unload (“detach”) the nlme package

Solution 1.3.1:

This is an Rstudio based exercise, so I can’t display the “Rstudio” images etc (well, I could if I’d had time to do all the screenshots and embed them in this document, but I haven’t had a chance to do it). Instead, here’s the R command that I would use to do the same thing from the R console:

library( nlme )

Exercise 1.3.2: Installing packages

There’s a few packages we’ll need for the rest of the workshop. So, for this exercise, use the “install packages” button in the Rstudio package tab to install the following packages:

  • lsr : Companion to “Learning Statistics with R”, written by Dan Navarro
  • psych : Psychometrics and Personality functions, written by William Revelle
  • car : “Companion to Applied Regression”, written by John Fox

It’s worth installing the ez package too: we won’t use it for the exercises, but it’s a good package for repeated measures ANOVA, and I do talk about it later on.

Solution 1.3.2:

install.packages( "lsr" )
install.packages( "psych" )
install.packages( "car" )
install.packages( "ez" )

Now that the packages are installed, I’ll load the lsr package because I’m going to use one of its functions later in the solution set:

library(lsr)

Exercise 1.3.3: Loading an .Rdata file

The data set that we’ll use for the exercise is located online at

http://ua.edu.au/ccs/docs/lsr/driving.Rdata

Open a browser, go to that address and save the file to disk. In my case, I saved it to

~/Work/Research/Rbook/csiro_workshop/datasets/driving.Rdata

where ~ is a standard symbol used in Unix-like operating systems such as Mac OS X that refers to “the current user’s home directory”, which for my current machine would be /Users/dan. I mention this because you’ll sometimes see R refer to the ~ directory, and you might want to know what it means.

In any case, once you’ve saved the file to disk somewhere, use the load button in the Rstudio workspace tab to open it. You’ll see that there’s only one variable there, a data frame called driving.

Solution 1.3.3:

Here’s the R command that I would use to do the same thing from the R console. First, I’ll use the who() function from the lsr package to print out the current state of the workspace. It’s basically a text version of the workspace viewer in Rstudio. Initially, we have nothing in the workspace:

who()
## No variables found

Now we load the file:

load( "../datasets/driving.Rdata" )

Notice that I’ve specified the file location in a slightly different way to how Rstudio does it. If you don’t already know this notation, .. means “the directory above the current one”. That’s kind of handy for the purpose of this solution set, because I’ve stored the exercise files and the data files in two different folders that both belong to the csiro_workshop folder. This way, if I ever move the csiro_workshop folder, the link won’t break. But it’s not important for our purposes. If you’ve loaded the file using Rstudio, it will specify the file in some unambiguous way (unambiguous to the computer, anyway) and will create the appropriate load() command.

Having loaded the data, we can now look at the state of the workspace:

who() 
##    -- Name --   -- Class --   -- Size --
##    driving      data.frame    17 x 9

Yep, we now have our driving data frame.

Exercise 1.3.4: Saving the current workspace to a file

Create a few new variables, and then use the save button in the Rstudio workspace tab to save it to disk. Don’t overwrite the existing driving.Rdata file: call it something like driving_version2.Rdata or something. I generally find that it’s a good idea to leave the original data file in pristine condition.

Solution 1.3.4

Again, this is an Rstudio based exercise. Here’s the R commands

blah <- "blahblah"
something <- 0
save.image( "../datasets/driving_modified.Rdata")

Here’s the variables that have been saved in the modified data file

who()
##    -- Name --   -- Class --   -- Size --
##    blah         character     1         
##    driving      data.frame    17 x 9    
##    something    numeric       1

Exercise 1.3.5: Importing a CSV file

There is a CSV version of the driving data frame located at

http://ua.edu.au/ccs/docs/lsr/driving.csv

Save it to disk, and import the data using the “import dataset” menu in the Rstudio workspace tab.

Solution 1.3.5

Again, here’s the R based solution rather than the Rstudio version:

driving <- read.csv("../datasets/driving.csv" )

Bonus 1.3.5:

In case you ever need to do it, here’s how you write a data frame to a CSV file. R has a function called write.csv() that you can use

write.csv( driving, file="../datasets/driving.csv", row.names=FALSE )

Exercise 1.3.6: Writing a script

  • use the Rstudio toolbars to open a new script
  • use the save button to save it to disk (call it something like newscript.R)
  • write some comments at the top of the script indicating what the script is for (e.g., “this is for a class exercise”), who wrote it (you), and when it was last modified (today). This is a good habit to get into.
  • then add some R commands. Any old commands will do.
  • save it again.
  • click on the “source” button to run the script.

Solution 1.3.6

Again, I can’t show the Rstudio aspects to the solution. But here’s what I wrote in my script:

# A script to illustrate the solution to 1.3.6
# Author: Dan Navarro
# Last modified: 27 Nov 2013

one <- "the loneliest number is"
print( one )

print( 6-5 )

Here’s what happens when I source the script:

source( "../datasets/newscript.R" )
## [1] "the loneliest number is"
## [1] 1