1. A Sample Session#

This section is an introduction to the R programming language. It shows how to perform very simple tasks using an R programming environment of your choice. Two possible choices of environment options are jupyter and RStudio. Once you have installed an R environment on your computer you can simply click on the copy button on these web pages and then paste the R code into your own system.

Note that these notes were primarily developed with jupyter in mind, so the concepts of “notebook” and “cell” are often refered to. However, this will not prevent you from using these notes in conjunction with an R environment other than jupyter.

Good luck !!

1.1. Getting Started#

To use R in jupyter you can type R commands into a code cell and then run them by pressing crtl-return. For example -

2+3
5
log(3.14)
1.14422279992016

Notice that the output produced by the R code is displayed below the cell once the cell has been executed by pressing ctrl-return. A cell can contain more than one line of code, for example -

sin(4.2)^2/4
log(exp(5))
4*atan(1)
0.189911081764586
5
3.14159265358979

R is an object orientated programming language in which pretty much everything is an object with a particular type associated with it. An instance of an object can be referenced through a variable. For example

x<-3.14
message<-"hello world"
v<-c(1,2,3,4)

Notice that executing this cell does not produce any output. The following one will though.

x
message
v
3.14
'hello world'
  1. 1
  2. 2
  3. 3
  4. 4

In this case the variable x refers to an object of type double with value 3.14. Notice that the <- operator was used to associate a variable with an object. You can also use = to do this. To determine the type of an object referred to by a variable x you can use the typeof function.

typeof(x)
'double'

1.1.1. Optional material 1 - Names and values#

The relationship between variables and values is well explained by Hadley Wickham in his highly regarded book Advanced R. See section 2.2 in the chapter names and values.

1.1.2. Exercise 1#

Find out what R types message and v are ?

Hide code cell source
typeof(message)
typeof(v)
Hide code cell output
'character'
'double'

Many of the common mathematical functions available in R will work with vectors of values. For example

y<-sin(v)
cat(y)
0.841471 0.9092974 0.14112 -0.7568025

1.1.3. Exercise 2#

You can get information about a function using the help function. Open up a new R session in jupyter and run the following command to see what happens.

help(cat)

1.2. Loading packages#

There are many “add ons” for R. The add ons are contained in packages. A package can be loaded into an R session using the library function. For example, there is a package called MASS. It can be loaded as follows.

library(MASS)

You can also use the library function to obtain information about a library. Try loading the MASS package using the library function in your jupyter notebook and find out what it kind of things it offers.

library(help="MASS")

Try loading the MASS package using the library function in your jupyter notebook and find out what it kind of things it offers.

There are many many packages for R, and you can make your own (not covered in this course). Most of them do not come as part of a standard R installation, but they are typically available from various online repositories, for example, CRAN, or frequently from github repositories.

Before you can load a package it has to be installed on your system. If it is not yet installed you can install it using the install.packages function. Note that this is not the only way, but it works well for the large number of generally well maintained packages that are available from CRAN. For instance, the zoo package is not usually part of an R installation so might not be installed on your system.

1.2.1. Exercise 3#

Try running the following code in your notebook.

library("zoo")
Attaching package: ‘zoo’
The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Using the install.packages function you can install zoo from CRAN.

install.packages("zoo")
Installing package into ‘/home/grosed/R-notes-env/R-packages’
(as ‘lib’ is unspecified)

Note that, once a library has been installed, it can be loaded using the library function and does not need to be reinstalled each time you use it. Packages can also be uninstalled using the remove.packages function.

You should now be able to load the zoo package.

library(zoo)

Some packages (but not all) have a vignette associated with them. Once the package is installed and loaded these vignettes (if they exist) can be accessed using the vignette function.

vignette("zoo")

There are functions that help you search for functions. These include apropos and find.

apropos("mean")
  1. '.colMeans'
  2. '.rowMeans'
  3. 'colMeans'
  4. 'kmeans'
  5. 'mean'
  6. 'mean.Date'
  7. 'mean.default'
  8. 'mean.difftime'
  9. 'mean.POSIXct'
  10. 'mean.POSIXlt'
  11. 'rollmean'
  12. 'rollmean.default'
  13. 'rollmeanr'
  14. 'rowMeans'
  15. 'weighted.mean'
find("rollmean")
'package:zoo'

It is also possible to search across packages that are available from the main repositories using the RSiteSearch function.

RSiteSearch("runmed")
A search query has been submitted to https://search.r-project.org
The results page should open in your browser shortly

1.3. Managing an R session#

There are some basic functions to help you manage the variables, data, packages in your R session. The three main ones are

  • ls lists the objects in your workspace.

  • list.files lists the files located in the folder’s workspace

  • rm removes objects from your workspace; rm(list = ls()) removes them all.

For example

ls()
  1. 'message'
  2. 'v'
  3. 'x'
  4. 'y'
rm(y)
ls()
  1. 'message'
  2. 'v'
  3. 'x'

Note that

rm(list=ls())

removes ALL of your data and variables !!

ls()

The sessionInfo gives information about your session, i.e., loaded packages, R version, etc.

sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 24.04 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/London
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] zoo_1.8-12      MASS_7.3-60.0.1

loaded via a namespace (and not attached):
 [1] digest_0.6.36     IRdisplay_1.1     utf8_1.2.4        base64enc_0.1-3  
 [5] fastmap_1.2.0     lattice_0.22-5    glue_1.7.0        htmltools_0.5.8.1
 [9] repr_1.1.7        lifecycle_1.0.4   cli_3.6.3         fansi_1.0.6      
[13] grid_4.3.3        vctrs_0.6.5       pbdZMQ_0.3-11     compiler_4.3.3   
[17] tools_4.3.3       evaluate_0.24.0   pillar_1.9.0      crayon_1.5.3     
[21] rlang_1.1.4       jsonlite_1.8.8    IRkernel_1.3.2    uuid_1.2-0       

1.4. Loading and saving data#

You can save objects to a file using the save function …

x<-seq(1,10,0.1)
ls()
'x'
save(x,file="some.data.Rda")
rm(x)
ls()

… and load it using load

load(file="some.data.Rda")
ls()
x
'x'
  1. 1
  2. 1.1
  3. 1.2
  4. 1.3
  5. 1.4
  6. 1.5
  7. 1.6
  8. 1.7
  9. 1.8
  10. 1.9
  11. 2
  12. 2.1
  13. 2.2
  14. 2.3
  15. 2.4
  16. 2.5
  17. 2.6
  18. 2.7
  19. 2.8
  20. 2.9
  21. 3
  22. 3.1
  23. 3.2
  24. 3.3
  25. 3.4
  26. 3.5
  27. 3.6
  28. 3.7
  29. 3.8
  30. 3.9
  31. 4
  32. 4.1
  33. 4.2
  34. 4.3
  35. 4.4
  36. 4.5
  37. 4.6
  38. 4.7
  39. 4.8
  40. 4.9
  41. 5
  42. 5.1
  43. 5.2
  44. 5.3
  45. 5.4
  46. 5.5
  47. 5.6
  48. 5.7
  49. 5.8
  50. 5.9
  51. 6
  52. 6.1
  53. 6.2
  54. 6.3
  55. 6.4
  56. 6.5
  57. 6.6
  58. 6.7
  59. 6.8
  60. 6.9
  61. 7
  62. 7.1
  63. 7.2
  64. 7.3
  65. 7.4
  66. 7.5
  67. 7.6
  68. 7.7
  69. 7.8
  70. 7.9
  71. 8
  72. 8.1
  73. 8.2
  74. 8.3
  75. 8.4
  76. 8.5
  77. 8.6
  78. 8.7
  79. 8.8
  80. 8.9
  81. 9
  82. 9.1
  83. 9.2
  84. 9.3
  85. 9.4
  86. 9.5
  87. 9.6
  88. 9.7
  89. 9.8
  90. 9.9
  91. 10

1.5. Extra exercises#

1.5.1. Extra Exercise 1#

Install and load the runner package from CRAN.

1.5.2. Extra Exercise 2#

What does the runner package do ?

1.5.3. Extra Exercise 3#

Does the runner package have a vignette ?

1.5.4. Extra Exercise 4#

Run some of the examples for the runner function in the runner package in your jupyter notebook.