Working with R - Answers

Preface

Open Rstudio to do the practicals. Note that tasks with * are optional.

R packages

In this practical, a number of R packages are used. The packages used (with versions that were used to generate the solutions) are:

  • survival (version: 3.7.0)

R version 4.4.2 (2024-10-31 ucrt)

Dataset

For this practical, we will use the heart and retinopathy data sets from the survival package. More details about the data sets can be found in:

https://stat.ethz.ch/R-manual/R-devel/library/survival/html/heart.html

https://stat.ethz.ch/R-manual/R-devel/library/survival/html/retinopathy.html

Basic calculations

Expressions

Assignment

Now use assignment to do the same calculation. Assign names a, b and c to each of the three values involved, then do the calculation while assigning the name d to the result.

Execute the following code.

a <- 45
b <- 2
c <- 3
d <- (a - b) * c 

a, b, c, and d are variables. To see their values, you can just type the variable name (e.g. a) and hit Ctrl-Enter or use the command print(a).

a <- 45
b <- 2
c <- 3
d <- (a - b) * c 
print(a)
[1] 45
print(b)
[1] 2
print(c)
[1] 3
print(d)
[1] 129
d
[1] 129

Combine the two values 3 and 4 into a vector v1 using the c function. Assign the name v1 to the vector. Then calculate the square of each value in the vector. Create a vector v2 consisting of the numbers 1 and 2 and calculate the sum of v1 and v2.

Execute the following code.

v1 <- c(3, 4)
# now compute the square of each value
# create v2 in the same way as v1 and calculate the sum
v1 <- c(3, 4) # the c(.) fucntion combines the arguments 3 and 4 into a vector
v1^2
[1]  9 16
v2 <- c(1, 2)
v1 + v2
[1] 4 6

Create a vector of numbers using the function call seq(1, 9). Theck that 1:9 does the same thing. What do seq(1, 9, by=2) and 9:1 do?

# Just type in the given code fragments and see what they do.
seq(1, 9)
[1] 1 2 3 4 5 6 7 8 9
1:9
[1] 1 2 3 4 5 6 7 8 9
seq(1, 9, by=2)
[1] 1 3 5 7 9
9:1
[1] 9 8 7 6 5 4 3 2 1

Loading packages and using the help function

Loading packages

We will use data sets from the survival package. It is important to know how to load packages as they contain most of the functionality of R.

Getting help

Importing and Saving Data

Save your work

It is important to save your work. You can save the whole workspace using the function save.image. If you want to save only specific objects, you can use the function save.

Load your work

Let’s continue working on the data sets by loading our results.

At this point it is interesting to check the objects that are in the workspace. To do so look at the Environment tab in the upper-right hand pane. You can also use the ls() function to list all objects in the workspace.

Remove your work

Remove unnecessary objects.

At this point you can inspect the History tab in the upper-right hand pane to see the commands you have executed. If you have made a syntax file check if you can run it in it’s entirety without any errors (if you have not you can piece it together from the history). Save the file to disk.