1 Exercises
1.1
Create a numeric variable a
using the R code below:
a <- 9
Run a
to verify that your code has worked properly.
1.2
To test whether a
is indeed a numeric variable, we can use the function is.numeric
. Run the R code below, and interpret the output.
is.numeric(a)
1.3
Next, create a second object in R called b
, and store the value 8 in this object.
1.4
Using *
, multiply a
by b
in R, and assign the square root of the result to a third object called c
.
Run c
and is.numeric(c)
to verify that your code has worked properly.
1.5
Suppose that we would like to round the value stored in c
to 3 decimal places. Rather than doing this by hand, we can use the built-in R function round
.
Throughout this subject, we will use many different R functions. Each will work differently, so it is always a good idea to check how to use a function before applying it to your data. The function is.numeric
was fairly straightforward to understand (outputting true or false), but the function round
is a little more complicated, as it requires multiple inputs aka arguments.
To bring up some helpful information on a function, you can type ?
or help()
and then the name of the function, and then run that line of code - try running the R code below for the round
function:
?round
help(round)
1.6
Following what you have learned from the round
help file in 1.5, use the round
function to round the value stored in c
to 3 decimal places.
What is your final answer for c
?
1.7
Use the function seq
to create a vector called fives
of values from 0 to 40, which increase in increments of 5 (i.e. 0, 5, 10, 15, …).
After creating your vector, run fives
to verify that it looks how you expect it to look. If something seems wrong with your result and you are not sure how to fix it, check with your demonstrator.
1.8
Next, create a vector containing the names of the following countries: Australia, Singapore, China, New Zealand, Japan and India. Name this vector countries
.
1.9
Now create a vector called pop.millions
containing the values 25, 5, 1433, 4, 126 and 1366, in that order.
1.10
Use the function cbind
to create a matrix containing the vectors fives
and pop.millions
. Make sure to name this matrix (the name can be whatever you like). What do you notice about this matrix?
1.11
Check the dimensions of your matrix using the dim
, nrow
and ncol
functions.
1.12
Create a data frame called countries.df
, containing the vectors countries
and pop.millions
.
1.13
Next, create a list called my.list
, containing the variable c
, the vectors fives
and countries
, and the data frame countries.df
.
Note: Remember, unlike data frames, lists can contain objects of different types. However, as discussed in section 6.7 of Book 2: R Coding Fundamentals, the original names of the objects included in the list get discarded, so we need to assign the names we want to each object in our list.
Once you have created your list, use the names
R function to check the name of each object in the list, to verify your code has worked as intended.
1.14
In this computer lab, we have focused on using R to create our own small data sets, using matrices, data frames and lists. However, we will often want to analyse pre-existing data sets, or use specific functions that are not loaded in base R.
In such instances, we will often need to use add-on R packages.
Many R packages are installed alongside R, but need to be loaded into our current session if we would like to use them. One such package is MASS
, which we will look at in more detail in Computer Lab 2.
To load a package in R, we can use the library
function.
Try loading the MASS
package now.
1.15
Some add-on R packages will not have been installed alongside R, and so we will need to download and install them before we can use them.
The majority of add-on R packages can be found on CRAN, the Comprehensive R Archive Network. One such package, which we will use throughout the semester, is the palmerpenguins
R package (Horst, Hill, and Gorman 2020). This package contains a data set on penguins living in the Antarctic.
To install an R package from CRAN, we use the function install.packages
. Run the code below to install the palmerpenguins
package now.
install.packages("palmerpenguins")
Once the package is installed, we can now load it into our current session. Load the palmerpenguins
package now, following the same process you used in 1.14.
1.16
Use what you have learnt in 1.5 to take a look at the help file for the palmerpenguins
package.