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:
Note: Make sure you review the assigned chapter reading + tutorial 1 video as it explains these concepts (and many more) in much greater detail.
This icon is what you need to click to open RStudio.
This is how a typical RStudio Interface looks:
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:
Before you actually start typing code, lets review the basic data types discussed in tutorial.
The basic R Values (or data types) are:
7
## [1] 7
"Hello this is DISC420"
## [1] "Hello this is DISC420"
TRUE
## [1] TRUE
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
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 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:
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
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:
This is definitely based on a true story:
number_a <- 1
numner_b <- "seven"
number_a + number_b
## Error in eval(expr, envir, enclos): object 'number_b' not found