Introduction

This is your first practice exercise.To start off you will be working in RStudio. RStudio is an integrated development environment (IDE). It’s super user friendly and makes it easier for us to write and execute code in R. Here is what you will be practicing today:

Game plan:

  1. Open RStudio
  2. Make sense of RStudio’s Interface
  3. Create a New Script File
  4. Review Basics
  5. Tasks

Note: Make sure you review the assigned chapter reading + tutorial 1 video as it explains these concepts (and many more) in much greater detail.


1. Open RStudio

This imageicon is what you need to click to open RStudio.

2. Make sense of RStudio’s Interface

This is how a typical RStudio Interface looks: Script


3. Create a new script file

In this class, we will only be working on script files. All assignments and exams will require the submission of SCRIPT FILE ONLY (unless an additional file is required). The tell sign of a script file is the .R extension. Any other file (R Markdown, R Notebook, R Shiny etc.) will NOT be submitted or graded for this class.

To open a new script file here is what you do:

Once you have selected R Script this is what you will see:

Note: You can also use a keyboard shortcut (mac: cmd+shift+N, window: ctrl+shift+N) to open a new script file.

Make sure that you save the file as Name_XXXX.R where Name is your FirstLast Name and XXXX are the last four digits of your student ID.

Make sure you always check the extension of your file before you submit. It needs to be .R.


Now that you have your script file, it is time to start typing some code. To run a line of code in your script file type the line of code and then click Run icon.

Alternatively, use the keyboard shortcut to save time:

  • Windows: ctrl+ enter
  • Mac: cmd + enter

4. Review Basics

Data Types:

Before you actually start typing code, lets review the basic data types discussed in tutorial.

The basic R Values (or data types) are:

  1. Numerics
7 
## [1] 7
  1. Characters
"Hello this is DISC420"
## [1] "Hello this is DISC420"
  1. Logicals
TRUE 
## [1] TRUE
Expressions:

Expressions are built up from values. These are the sentences that the language understands and thus evaluates.

10+9
## [1] 19
8^2
## [1] 64
4/2
## [1] 2
Logical Expressions:

These result in TRUE or FALSE statements. When run, the following lines of codes give these results:

3>2
## [1] TRUE
3>9 
## [1] FALSE
Variables:

Variables are the names we use to store data and then later refer to said data. The style convention in R is to use the assignment operater which looks like this <-. You can also use keyboard shortcut: Windows: (Alt+-) Mac: (Option+-) to make this operator. We use <- to assign values to a variable as follows:

x <- 10 # assigns value to x
b <- 20 # assigns value to b
x+b # the values of x and b are stored, now we can use them to make more expressions
## [1] 30

Note:

  • Did you notice the #s? #Hashtags are used to make comments in your script file. Comments are very important aspects of your code as they help reader understand the thought process behind your code. Get in the habit of making lots of comments on your scripts.
  • Please refer to tutorial 1 video for detailed discussion regarding naming conventions for variables.
In-built Functions in R:

Concept:

Think of the function y = x + 1. Here x is the input and y is the output . In R a function is a pre-written piece of code that performs a specific task. It takes an input (arguments) , processes them, and returns an output or result.

An important in-built function you can use to check the data type of any object is the class function. For example:

a <- 5
class(a)
## [1] "numeric"

General Function Syntax

  • Function Name : The name of the function that describes the operation it performs (eg: class)
  • Arguments : Functions often require one or more arguments enclosed in parentheses. Arguments are input values that the function works on (e.g., class(a)) where a is the argument).
  • Output : Functions typically return a value as output (e.g., the class(a) is “numeric”).

There are loads of in-built R Functions that will make our lives easier and help us perform data manipulation/calculations/analysis/visualizations etc.


Okay, now that you have reviewed some of the basics lets actually get to coding!

Reminders:

  1. Make sure you are working in your script file
  2. When you write a line of code make sure to RUN it and check the output in your console window.
  3. Keep saving your script file RELIGIOUSLY.

This is definitely based on a true story:

Tasks:

  • Task 1: Create a variable called “current_age”. Assign this variable the value of your age (in whole number)
  • Task 2: Create a variable called “birth_month”. Assign this variable with the month of your birthday
  • Task 3: Create a variable “age_in_5_years”. Assign this variable with current_age + 5 years
  • Task 4: Check the data type of current_age
  • Task 5: Check the data type of birth_month
  • Task 6: What is the error in the following code:
number_a <- 1
numner_b <- "seven"
number_a + number_b
## Error in eval(expr, envir, enclos): object 'number_b' not found

Bonus Reading on Stylistic Guidelines