During this lab, you will get familiar with the most important tools that will be used during the course. These tools include (Google Colab) for Python and (RStudio) for R.
Google Colab for Python
For simplicity, we will use Google Colab for running the Python programs during the course. If you are familiar with other Integrated Development Environment such as Anaconda, PyCharm, VScode or Spyder, then use that IDE. Be careful that specific libraries may run only under a specific version of Python.
Google Colab
We will start by creating a new notebook and checking the version of Python that is already installed. We run the command:
!python --version
If we need to install a different version of Python (e.g. 3.7), we can use:
!apt-get install python3.7
Installing more Libraries
Most of the important libraries that we may need are already installed in the Google Colab environment. However, if you would like to install additional libraries, you can use the pip command. For example, to install py_stringmatching, you can use the command:
!pip install py_stringmatching
Note: when running system commands in Google colab, we use ! before the command (this is common for running system commands in any notebook environment).
Running simple Python code
Run the following Python code and explain what the code is doing:
t, f =True, Falseprint(t and f) print(t or f) print(not t)
R & RStudio
We will be using the most popular development environment for R: RStudio. In the exercises below (and the readings for this lab from R4DS) you will set up your computer for doing the R practicals in later parts of this course.
R & RStudio
Installing R & RStudio
Install R and RStudio as per the instructions in the syllabus here.
Installing additional packages
Open RStudio, find the console window (the REPL) and type the following code:
2+2
This should return 4.
If you are not sure where to execute code, use the following figure to identify the console:
To install packages in R, we use R directly (unlike in python where we commonly use the pip module). Install the tidyverse suite of packages.
install.packages("tidyverse")
If you are asked
Do you want to install from sources the package which needs
compilation? (Yes/no/cancel)
type no in the console and press the return key.
Did this all go well? Explain to your neighbour what just happened. What is tidyverse?
Required R knowledge
The following is the minimum of what you should know about R before starting with the first R practical later in the course. Take some time to explore these points! Look up things on the internet if you are unsure.
What is R (a fancy calculator) and what is an .R file (a recipe for calculations)
What is an R package (a set of functions you can download to use in your own code)
How to run R code in RStudio
What is a variable x <- 10
What is a function, e.g., y <- sqrt(x = 23)
Understand what the following statements do (tip: you may run it in R line by line)
y <-"What?"x <-"R!"z <-paste(x, "Data Analysis and Machine Learning is cool!", y)rep(z, 3)1:10sample(1:20, 4)sample(1:20, 40, replace =TRUE)z <-c(1, 2, 3, 4, 5, 4, 3, 2, 1)z^2z ==2z >2
Be able to read the help file of any function, (e.g., type ?plot in the console)