45-2*3
45-2)*3
(#etc
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
Use R as a calculator to calculate the following values:
- \(45 - 2 \times 3\)
- \((45 - 2) \times 3\)
- \(17 ^4\),
- \(2 ^ (1/3)\),
Enter the calculations in an R-script and then run the lines using .
45-2 * 3
45-2) * 3
(17^4
2^(1/3)
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.
<- 45
a <- 2
b <- 3
c <- (a - b) * c d
a
, b
, c
, and d
are variables. To see their values, you can just type the variable name (e.g. a
) and hit Ctrl-EnterCtrl-Enter or use the command print(a)
.
<- 45
a <- 2
b <- 3
c <- (a - b) * c
d 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.
<- c(3, 4)
v1 # now compute the square of each value
# create v2 in the same way as v1 and calculate the sum
<- c(3, 4) # the c(.) fucntion combines the arguments 3 and 4 into a vector
v1 ^2 v1
[1] 9 16
<- c(1, 2)
v2 + v2 v1
[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.
Load the survival
package using the library
function.
Use the library(...)
function. There should be no need to install the package as it is already installed with R
.
library(survival)
# library("survival") is an alternative in this case
# require(survival) is another alternative which does not give an error if the package cannot be found
Getting help
Ask for the documentation of the heart
data set, using the help
function. Then do the same for the retinopathy
data set using ?
.
Use the help(...)
function and ?
.
help("heart")
?retinopathy
Also ask for help on the c
, ls
, rm
and seq
functions. Check if you can also obtain the documentation for the c
function if you use ??combine
Use the help(...)
fuction, and ?
or ??
.
# or use help("...")
?c
?ls
?rm
?seq
??combine# or use help.search('...')
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
.
Save the vectors numbers <- c(34, 24, 19, 23, 16)
, numbers_2 <- c(1:200)
and treatment <- c("yes", "yes", "no", "no", "no", "yes")
. Use the name new_vectors
.
Use the function save(...)
. Note that you need to set the working directory.
<- c(34, 24, 19, 23, 16)
numbers <- c(1:200)
numbers_2 <- c("yes", "yes", "no", "no", "no", "yes")
treatment save(numbers, numbers_2, treatment, file = "new_vectors.RData")
Save the vectors events <- heart$event
and eyes <- retinopathy$eye
. Use the name vectors_survival
.
Use the function save(...)
. Note that you need to set the working directory.
<- heart$event
events <- retinopathy$eye
eyes save(events, eyes, file = "vectors_survival.RData")
Load your work
Let’s continue working on the data sets by loading our results.
Load the file new_vectors
.
Use the function load(...)
.
load("new_vectors.RData")
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.
Remove the vectors numbers
, numbers_2
and treatment
.
Use the function rm(...)
.
rm(numbers, numbers_2, treatment)
Remove the vectors events
and eyes
.
Use the function rm(...)
.
rm(events, eyes)
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.