R Coding Fundamentals


In our first Computer Lab, we will work through some simple exercises to reinforce the material we have just covered in Book 2: R Coding Fundamentals, of the Introduction to R content.

Please note that any references in this computer lab to Books 1-3 refer to the Introduction to R books.

If you have not already watched the R introduction video, please make sure to do so now, before you begin working on this computer lab.


If you have time at the end of today’s lab, you may like to work on the Quiz 1.

Preparation

Open up RStudio and create a new script file.

You can copy and paste the code presented in each of the questions below into this script file, to help you get started.

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.


For help answering this question, see Section 2.3 of Book 2: R Coding Fundamentals, or Video 5. The Assignment Operator (for details on the assignment operator.). For details on variables, see Sections 2.6.1-2.6.3 of Book 2: R Coding Fundamentals, or Video 9. Variables. These resources are all available on LMS.

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)


For help answering this question, see Section 2.3.1 of Book 2: R Coding Fundamentals, or Video 6. Logical Operators, on LMS.

1.3

Next, create a second object in R called b, and store the value 8 in this object.

Hint: Check the code above in 1.1 if you are not sure how to proceed.

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.


For help answering this question, see Section 2.2 of Book 2: R Coding Fundamentals, or Video 4. Basic Mathematical Operations, on LMS.

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)


For help answering this question, see Section 1.4 of Book 1: Getting Started with R, or Video 2. Help and Support, on LMS.

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?


For help answering this question, see Section 2.5 of Book 2: R Coding Fundamentals, or Video 8. Functions, on LMS.

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, …).

Hint: We have filled in some of the code below to help get you started. The … denote missing details which you will have to fill in. If you would like some extra help on how to use the seq function, apply what you learnt in 1.5.

fives <- seq(from = ... , to = ..., ... = ...) 

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.


For help answering this question, see Section 2.6.4 of Book 2: R Coding Fundamentals, or Video 10. Vectors, on LMS.

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.

Hint: Remember, because these are character variables rather than numeric variables, you will need to enclose each country name in quotation marks. Also, the c function will be useful.

For help answering this question, see Video 10 (Vectors), or Section 6.4 of Book 2: R Coding Fundamentals on LMS.

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?

Hint: Check back to the process outlined in 1.5 if you are not sure how to use the cbind function.


For help answering this question, see Section 2.6.5 of Book 2: R Coding Fundamentals, or Video 11. Matrices, on LMS.

1.11

Check the dimensions of your matrix using the dim, nrow and ncol functions.

Hint: Check the code below if you would like some guidance.

# Remember, if you need more details about an R function, 
# e.g. the dim function, you can run the line
?dim
# To find the dimensions of our matrix, we can use the following code:
dim(mat)
# Note here we are assessing a matrix named mat - you may have named your matrix differently

1.12

Create a data frame called countries.df, containing the vectors countries and pop.millions.

# This code creates a data frame consisting of the imaginary vectors 'cats' and 'dogs'. 
# Try using this as a base for creating your data frame.
example.df <- data.frame(cats, dogs)


For help answering this question, see Section 2.6.6 of Book 2: R Coding Fundamentals, or Video 12. Data Frames, on LMS.

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.

Hint: Check the code below for a head start if you are not sure how to do this.

# For this example list, we have the two vectors 'cats' and 'dogs', which we would like to include in our list,
# with the names 'cat.data' and 'dog.data'.
example.list <- list(cat.data = cats, dog.data = dogs)
# Try using this as a base for creating your 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.


For help answering this question, see Section 2.6.7 of Book 2: R Coding Fundamentals, or Video 13. Lists, on LMS.

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.

Hint: Use what you have learnt in the previous steps of the computer lab to help you determine how to use the library function.


For help answering this question, see Section 1.5 of Book 1: Getting Started with R, or Video 3. R Packages, on LMS.

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")
# Note that the package name must be surrounded by quotation marks when using this function.

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.


Next Steps: Next up, please take a look at Book 3: Importing and exporting data.

That’s everything for today! Hopefully you now feel more confident using R. Don’t worry if it seems difficult at the moment - we will have plenty of time to practice and improve as the semester progresses.

Before you finish up, make sure to save your script file somewhere safe - it might come in handy later on.

If there were any parts you were unsure about, take a look back over the relevant sections of the Introduction to R books.


References

Horst, Allison Marie, Alison Presmanes Hill, and Kristen B Gorman. 2020. Palmerpenguins: Palmer Archipelago (Antarctica) Penguin Data. https://doi.org/10.5281/zenodo.3960218.


These notes have been prepared by Rupert Kuveke and Amanda Shaker. The copyright for the material in these notes resides with the authors named above, with the Department of Mathematics and Statistics and with La Trobe University. Copyright in this work is vested in La Trobe University including all La Trobe University branding and naming. Unless otherwise stated, material within this work is licensed under a Creative Commons Attribution-Non Commercial-Non Derivatives License BY-NC-ND.