17^4
2^(1/3)
Working with R - Practical
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.5.7)
R version 4.3.2 (2023-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:
- \(17 ^4\),
- \(45 - 2 \times 3\)
- \((45 - 2) \times 3\)
Enter the calculations in an R-script and then run the lines using Ctrl-EnterCtrl-Enter.
etc
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)
.
Importing and Saving Data
Save your work
It is important to save your work.
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.
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.
Load your work
Let’s continue working on the data sets by loading our results.
Load the file new_vectors
.
Use the function load(...)
.
Remove your work
Remove unnecessary objects.
Remove the vectors numbers
, numbers_2
and treatment
.
Use the function rm(...)
.
Remove the vectors events
and eyes
.
Use the function rm(...)
.