Common Objects - 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
Always check your data.
Explore the heart and retinopathy data sets - print the first six and last six rows.
Use the functions head(...)
and tail(...)
to investigate the data set. Replace the dots with the name of the data set.
Common R Objects
It is important to distinguish the different object in R.
Vectors
Let’s investigate some vectors.
View the vectors event
and age
from the heart data set.
Use the dollar sign to select the variables.
View the vectors eye
and risk
from the retinopathy data set.
Use the dollar sign to select the variables.
Create a numerical vector that consists of the values: 34, 24, 19, 23, 16. Give the name numbers
to this new vector.
Use the c(…) function. Replace the dots with the numbers.
Create a numerical vector that takes the integer values from 1 until 200. Give the name numbers_2
to this new vector.
Use the c(…) function. Replace the dots with the numbers.
Create a categorical vector that consists of the values: yes, yes, no, no, no, yes. Give the name treatment
to this new vector.
Use the c(…) function. Replace the dots with the categories.
Matrices and Data Frames
Let’s investigate some matrices and data frames.
Create a matrix using the vectors id
and age
from the heart data set. This matrix should have 2 columns where each column represents each variable.
Use the function matrix(…).
Create a data frame using the vectors id
, type
and trt
from the retinopathy data set. This data frame should have 3 columns, where each column represents each variable.
Use the function data.frame(…).
Arrays
Let’s investigate some arrays.
Create an array that consists of 2 matrices. Matrix 1 will consist of the values 1:4 and matrix 2 will consist of the values 5:8. Both matrices will have 2 columns and 2 rows.
Use the function array(…).
Give the name ar1
to the previous array. Furthermore, investigate the argument dimnames and change the names of the rows, columns and matrices.
Use the function array(…). Check the help page for the dimnames argument. Note that this must be in a list format.
Lists
Let’s investigate some lists.
Create a list using the vectors stop
from the heart data set and id
, risk
from the retinopathy data set. Give the names stop_heart
, id_reti
and risk_reti
.
Use the function list(…).
Create a list using the vectors numbers
, numbers_2
and treatment
. These variables can be taken from the exercise called Vectors
. Give the names: numbers
, many_numbers
and treatment
.
Use the function list(…).