Understand the use of RStudio (console, editor)
Understand the use of Rmarkdown (compiling, saving, editing)
Use of the console, using R as a calculator
Assigning objects
Using vectors
Understand the architecture and use of functions in R
RStudio consists of 4 windows, the bottom left one is the console. The console is…
Complete/replace/expand the below text! If you go to File > New File, you can select from different options. Try to open a new RScript. What is the difference between an RScript and an RMarkdown document? If you open a new RMarkdown document, you will find some default code. By compiling it and comparing the code with the output you will understand what the code bits mean. Then turn back to this RMarkdown file (you will see different tabs at the top with the files that are currently open). Let’s insert a first piece of code into this document. To do so, go to Insert > Rm which inserts an R code chunk. Here is a first one for you:
1 + 1
[1] 2
Note that the two options comment = NA, collapse = T
were added to the code chunk. This is not crucial, it just changes the appearance of the code slightly. Try what it looks like if you remove them.
Here is a simple example for calculating the body mass index in R
height <- c(1.7, 1.9, 1.59)
mass <- c(80, 92, 45)
bmi <- mass/height^2
Here is a general hint: when you edit this file, compile it often. This allows you to identify a potential error much quicker if suddenly the file fails to compile.
Let’s turn to the console for this. This means you place the cursor in the cosole (down below) and simply start typing some equations like the one above. Try to use all basic mathematical operators, brackets, but also some trigonometric functions and the square root (e.g. sin()
, sqrt()
. Those will be your first functions in R you are using. Once you have played around in the console, insert bits of text illustrated by code chunks HERE to explain how to use R as a calculator. Use the body mass index as shown in class as an example.
plot(bmi, col = "darkgreen")
Again using the example of the body mass index, shows how it is easier to assign objects (using the assignment operator <-
, shortcut: alt + hyphen) rather than work with the actual numbers. Insert code chunks to illustrate your explanations. Why is it unwise to use e.g. sin
as an object name?
Explain how using vectors makes life a lot easier. Use the body mass index to illustrate this, again, use R code chunks. Once you have completed the section below on functions, you can also show how you can use the function rnorm()
(random normally distributed numbers) to create a vector of any length with random numbers that follow a normal distribution.
Listen to my explanations during the lab. Try to summarise it here. Use lots of examples (R code chunks) to illustrate your text. Turn to R books or other (online) resources if you need help after the lab.
We have run through an example of how to write function ourselves using the body mass index example from above.
bmi <- function(mass, heiht) {
mass/height^2
}
bmi(mass = mass, heiht = height)
## [1] 27.68166 25.48476 17.79993
We have now created a function called bmi
which has two function arguments: mass
and height
where the user is to provide the respective data (coincidentally, in this case our input data that we have created previously have the same names as the function arguments).
The idea of this way of learning (using RMarkdown) is that you write your own little course manual. Every student has a different way of learning, so add to the above. For example, you could have a ‘Tips and Tricks’ section here: Have you noticed that to get <-
you can using the ‘alt’ and ‘-’ key combination? Can you ignore the message ‘do you want to save your workdirectory?’ once you close R? Do you know how to save your work to a memory stick?
Once you think you have completed this document and understood what is outlined as ‘learning targets’, you should replace my name with yours in the author section at the top!