Week 1 Completed File

Author

Biagio Palese

Intro to R

The following sections of the book (R for Data Science) used for the first portion of the course are included in the first week:

Getting Started aka Running code in R

To run and execute code inside a code chunk simply click the Run Code button or ..

make sure that your cursor is anywhere in the line that you want to execute and press Cmd(Mac)/Ctrl(Windows) + Enter to run a single line of code or highlight multiple line and then press Cmd/Ctrl + Enter. Let’s try with the below code

Packages installation

  • You will need to install the packages first before be able to use functions and datasets they contain.

  • This is a one time task unless you uninstall R & RStudio or you change your computer.

  • We will need to install a few packages, among them the most important is tidyverse. Tidyverse is the main package for the first part of the semester, it is a bundle package (meaning it contains multiple packages that have different purposes e.g., dplyr for manipulations, ggplot for visualizations).

  • Below you can see the code require to install packages. I already took care of packages installation to make sure we are all on the same page and to save time and resources 😎

Load packages

This is a critical task:

  • Every time you open a new R session you will need to load the packages.

  • Failing to do so will incur in the most common errors among beginners (e.g., ” could not find function ‘x’ ” or “object ‘y’ not found”).

  • So please always remember to load your packages by running the library function for each package you will use in that specific session 🤝

Need help?

If you are stuck promise me not to quit! and/or

Don’t be this guy!

You are a beginner so it will happen.. it still happens to me and many other experienced programmers.

Just pay attention to the error message and look at the below options to find a solution:

  • look into the R community or Stackoverflow

  • contact us in MS Teams (R Forum Channel)

  • leverage LLM (e.g., ChatGPT)

  • use help with R documentation (e.g., try ?median below)

  • check examples of how to use a certain function (e.g., try example(median) below)

NB: During the skill tests you can only use the last two options so make sure you master them!

Workflow basic script

You can use R as a calculator..

Creating Objects using the Assignment operator

But it can do much more.. so we need to learn how to create and manage object using the assignment operator.

<- is the assign operator and it means you are assigning value to an object/variable. You can then call and use the object created later.

How to manage your objects?

How to name your objects?

  • Object names must start with a letter (can’t start with a number )

  • Object names can only contain letters, numbers, _ and . (I highly recommend _ to separate words).

  • You want your object names to be descriptive and to use words that remind you what are the data contained in that object.

Now let’s check your object creation understanding. Given the object created above, what happen if I run the following lines; why?

NB R is case sensitive make sure to keep everything lower case and to use _ to separate multiple words. These tips will save you lots of time and troubles!

Activity 1: Create objects by using the assignment operator (5 minutes)

[Write code just below each instruction; finally use MS Teams R - Forum channel for help on the in class activities/homework or if you have other questions]

Using functions

Functions are the real reason why you should use R.

There are too many functions to go over or even try remember. In fact, each package will give you access to a different set of functions and sometimes they will use the same name (e.g., pay attention to conflict warnings after you load a package).

However, the good news is that you don’t need to memorize them but understand how they work.

Let’s start with a basic example, how can we create a sequence of numbers in R?

Now can we create a sequence of number from 1 to 10 but make sure just 5 numbers are included? Can we assign this sequence to an object “y”?

Now what if we want a sequence from 1 to 1000 all the numbers between them?

Activity 2: Use basic functions (8 minutes)

[Write code just below each instruction; finally use MS Teams R - Forum channel for help on the in class activities/homework or if you have other questions]

On completing your first R coding class!